Android Question HttpJob and Raw Data

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello community,

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:


In "body" , raw data, something like this:

JSON:
{
    "notification_url": "https://notreturn.com/res/mp.php",
    "external-reference" : "123456789",
    "items" : [
    {
        "title":"Payment",
        "quantity":1,
        "currency_id":"BRL",
        "unit_price":10.0
    }
    ]
    "payment_methods": {
        "excluded_payment_types": [
            { "id": "ticket" },
            { "id" : "bank_transfer" }
        ]
    }
    "back_urls" {
        "success":"https://blha1.com",
        "failure":"https://blha2.com",
        "pending":"https://blha3.com"
    }
    "auto_return": "all"
}

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"

does anybody have any experience with that?

Thanks!
 

KMatle

Expert
Licensed User
Longtime User
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
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 !!!!
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Yes. If you have e.g. XAMP installed for your test environment, you can use php to "log" the data, the server receives:

PHP:
<?php
$poststring = file_get_contents("php://input"); //get the string or whatever you send via poststring
echo 'Received: ' . $poststring;
?>

To log the headers you've set:
PHP:
<?php
foreach (getallheaders() as $name => $value) 
{
    echo "$name: $value\n";
}
?>

With job.GetString you get the echoed values.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Yes. If you have e.g. XAMP installed for your test environment, you can use php to "log" the data, the server receives:

PHP:
<?php
$poststring = file_get_contents("php://input"); //get the string or whatever you send via poststring
echo 'Received: ' . $poststring;
?>

To log the headers you've set:
PHP:
<?php
foreach (getallheaders() as $name => $value)
{
    echo "$name: $value\n";
}
?>

With job.GetString you get the echoed values.
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!
 
Upvote 0

Similar Threads

Top