I have a doubt here... Using httpJob I know how to send a POST using parameters map (postMultiPart) and even to send a file. But now I have a post request listed in PostMan constructed in this way:
Link:
How do I code this using traditional "map" architecture in httpjob.postmultipart ? (usually is something like this):
B4X:
Dim job as httpJob
Dim postMap as map
Dim itemsList as List
Dim item1 as Map
Dim link as string = "https://api.serviceprovider.xpto/preferences?token=3434-cdf3-sd2ws-fgrt54t-sdfdf" <- notice that I used the link with "token" parameter even in POST method
map.put("notification_url","https://notreturn...")
...
item1.put("title","Payment")
itemsList.initialize
itemsList.add(item1)
...
map.put("items",itemsList)
...
job.initialize("",me)
job.postMultipart(link,postMap,null)
...
Will that work ? (a multi level map simulating the raw json requested by the api in body)? If no, how is the code to send a json in "body" using httpJob (considering also that in postMan test the selected method is "POST", the link has the parameter access_token in it and the json data is configured in section "body->raw"
You need to use the built in JSONGenerator (search for it). Right now you are are sending a MAP Object (not the JSON which is a string). Use the map as an input and send the string.
You need to use the built in JSONGenerator (search for it). Right now you are are sending a MAP Object (not the JSON which is a string). Use the map as an input and send the string.
Thanks @KMatle . But how is the httpJob setup to send the json... Something like this?
B4X:
Dim mapPost As Map
... (constructing the map)
Dim link As String = "https://somelink.com/preferences?token=sxdge7d-dfer3-sdsds" <- full link with token given inline
Dim json1 As JSONGenerator
json1.Initialize(mapPost)
Dim jsonString As String = json1.ToString
Dim job As HttpJob
job.Initialize("",Me)
job.PostString(link,jsonString)
job.GetRequest.SetContentType("JSON") <- is this required???
Wait For (job) jobDone(job As HttpJob)
...
Thanks @KMatle . In this case I'm sending the data to a third party server over which I don't have control or access (specifically the card gateway provider). When I write my REST apis in php I prefer to use the postMultiPart command with maps in client side because this approach gives to me the variables to get directly in $_POST["xpto"] method.
Thanks again for all support for @Erel and @KMatle . That's it that makes the B4X community one of the most efficient and collaborative communities for programming in the web!