Android Question PostMultipart - Arrays of Arrays

fasilosman

Active Member
Licensed User
Longtime User
Hi I used multipartpost to post data using okHttpUtil2.
I used map for values/data. But I have to send map with a map (arrays of array).
The map(array) is working fine. But the arrays of array is not working(is not inserted)

Following is the code I used.
B4X:
curl -X POST https://example.com/wp-json/wc/v2/customers \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "email": "john.doe@example.com",
  "first_name": "John",
  "last_name": "Doe",
  "username": "john.doe",
  "billing": {
    "first_name": "John",
    "email": "john.doe@example.com",
    "phone": "(555) 555-5555"
  },

for the above curl I used following b4a code

B4X:
        Dim Bill As Map
        Bill.Initialize
        Bill.Put("first_name","John")
        Bill.Put("email","john.doe@example.com")
        Bill.Put("phone","(555) 555-5555")
       
        Dim D As Map
        D.Initialize
        D.Put("email","john.doe@example.com")
        D.Put("first_name",Home.CUSDetail.Get("Username"))
        D.Put("last_name",Home.CUSDetail.Get("Username"))
        D.Put("username",Home.CUSDetail.Get("Username"))
        D.Put("billing",Bill)
       
        Dim Job As HttpJob
        Job.Initialize("Register",Me)
        Job.Username = "ck_5Xxxxxxxxxxc0"
        Job.Password = "cs_f38xxxxxxxxxxfa7"
        Job.PostMultipart("https://example.com/wp-json/wc/v2/customers",D,Null)
        Job.GetRequest.SetHeader("Content-Type", "application/json")
        Job.GetRequest.SetContentEncoding("text/plain")

Main details (email,first name,last name,username) is inserted but the Billing information is not inserted
I searched but I couldn't found what I missed.

Can anyone please help me
 

OliverA

Expert
Licensed User
Longtime User
Actually, both Poststring and MultiPart work.

1) With Poststring, it looks like the JSON data that you pass to the API is not in the correct format. That's why your getting a 400 (client side) error message. What is it expecting? No clue, I never have used the WP-API (WordPress JSON/JSONP API). Someone with experience would need to help you or you need to read up more about what the WP-API expects in regards to JSON/JSONP (note the error message mentions something about "rest_missing_callback_param". That's an indicator of JSONP).

2) The PostMultipart works too, your just getting an error back that the user already is registered. Here MultiPart just takes apart your map and makes each map entry a part in the multi-part. Looks like the WP-API is perfectly happy with accepting data that way (and it does not expect a callback, as above). Note that you do not even need to use these two lines with PostMultiPart

B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json")
Job.GetRequest.SetContentEncoding("text/plain")

So B4A and Poststring and MultiPart work as expected. If you want to use Poststring, you need to familiarize yourself more with the JSON that WP-API expects. With both Poststring and MultiPart, you need to familiarize yourself in reading the error responses that are returned to you by the WP-API.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Actually, both Poststring and MultiPart work.

1) With Poststring, it looks like the JSON data that you pass to the API is not in the correct format. That's why your getting a 400 (client side) error message. What is it expecting? No clue, I never have used the WP-API (WordPress JSON/JSONP API). Someone with experience would need to help you or you need to read up more about what the WP-API expects in regards to JSON/JSONP (note the error message mentions something about "rest_missing_callback_param". That's an indicator of JSONP).

2) The PostMultipart works too, your just getting an error back that the user already is registered. Here MultiPart just takes apart your map and makes each map entry a part in the multi-part. Looks like the WP-API is perfectly happy with accepting data that way (and it does not expect a callback, as above). Note that you do not even need to use these two lines with PostMultiPart

B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json")
Job.GetRequest.SetContentEncoding("text/plain")

So B4A and Poststring and MultiPart work as expected. If you want to use Poststring, you need to familiarize yourself more with the JSON that WP-API expects. With both Poststring and MultiPart, you need to familiarize yourself in reading the error responses that are returned to you by the WP-API.

THANKS A LOT for the clear explanation. Now I have a clear Idea about it and I get to know where is the issue.
Thanks again
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Finally I mange to go with PostString with ContantType "application/joson".

ISSUE -

when I was trying to generate the Map data (array data) as json string using JSONGenerator, it gives the string staring and ending character with "[" & "]".
The server I tried is not accepting this as a json string. The server expects the json string should begin and end with "{" & "}". So I removed the invalid character and send the PostString with json data. It worked. :).

I'm not sure that, it is a problem with the server I use, or it is a common issue.

Thanks for for all who helped me with this issue.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It worked.
Awesome. BTW, for the record, the "rest_missing_callback_param" just means that a required parameter is missing. So ignore what I posted about it previously.

Pulled from source here:
B4X:
if ( ! empty( $required ) ) {
   return new WP_Error( 'rest_missing_callback_param', sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ), array( 'status' => 400, 'params' => $required ) );
}
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
when I was trying to generate the Map data (array data) as json string using JSONGenerator, it gives the string staring and ending character with "[" & "]".
The server I tried is not accepting this as a json string. The server expects the json string should begin and end with "{" & "}".
This means that you should have created a Map instead of a List.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
This means that you should have created a Map instead of a List.

This is my code. I created a map in a list and generate json for the list.
B4X:
Dim b As Map
    b.Initialize
    b.Put("first_name","ccccc")
    b.Put("address_1","ccccc")
    b.Put("phone","221222222")
   
    Dim D As Map
    D.Initialize
    D.Put("email","cccc@e.com")
    D.Put("username","cccc")
    D.Put("password","cccc")
    D.Put("first_name","ccc")
    D.Put("billing",b)
   
       
    Dim Lst As List
    Lst.Initialize
       
    Lst.Add(D)
       
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize2(Lst)
       
    Dim Data As String
       
    Data =JSONGenerator.ToString
   
    Log (Data)

and the result is like this

[{"billing":{"first_name":"ccccc","phone":"221222222","address_1":"ccccc"},"first_name":"ccc","username":"cccc","password":"cccc","email":"cccc@e.com"}]

the result starts and ends with "[" & "]". But the server is not accepting that as json string. It want the string starts and ends with "{" & "}".

Is my code correct or where am I wrong.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the result starts and ends with "[" & "]". But the server is not accepting that as json string. It want the string starts and ends with "{" & "}".
Read the answers given.... Especially the answer before you post.

Don´t use a list here as the server is not expecting a list.
Try it with a map in a map.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Read the answers given.... Especially the answer before you post.

Don´t use a list here as the server is not expecting a list.
Try it with a map in a map.


Yes. You are absolutely correct . I have to use map and map in a map. Thats what the server expecting.
Most of the examples of json used a list. Since I am a beginner I followed them, gave the error.
Now I got the idea whats happening. Problems give knowledge

Thanks to everybody helped me in this regard
 
Upvote 0
Top