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
 

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Does the script/programm support getting a map inside the data?
I can not see a problem in B4A Code. The server-Side must accept the data to be in this format

It says Billing is an object type.
Since it is working with the map "D" above. I though it should also work with Bill also.
I referred here https://woocommerce.github.io/woocommerce-rest-api-docs/#customer-properties
I have no idea where to check the server side is accepting it.
I have hosted in www.000webhostapp.com
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User

I tried like
JsonString = [{"password":"a","email":"newuser@gmail.com","username":"newuser"}]

B4X:
        Dim Job As HttpJob
        Job.Initialize("Register",Me)
        Job.Username = "ck_5761ffa9xxxxxxxxxxd44e662d9caf32d6b1452c0"
        Job.Password = "cs_f38b34d83dxxxxxxxxxb5be5b86e54ecdd1fc51fa7"
        Job.Poststring("https://example.com/wp-json/wc/v2/customers" , JsonString)
        Job.GetRequest.SetHeader("Content-Type", "application/json")
        Job.GetRequest.SetContentEncoding("text/plain")

Still not working. the data is not passed.

I am confused what I missed or in a wrong way.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Finally I found that - PostMultiPart is working but PostString (I tried using JSONGenerator) is not working.
Can anybody help me. I could not find answer
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
PostString does not encode your string and most likely the receiving end is expecting an encoding string. So try StringUtil's EncodeURL method and see what happens.

Code fragment:
B4X:
Dim su As StringUtils
Job.Poststring("https://example.com/wp-json/wc/v2/customers" , su.EncodeURL(JsonString, "UTF8"))
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
PostString does not encode your string and most likely the receiving end is expecting an encoding string. So try StringUtil's EncodeURL method and see what happens.

Code fragment:
B4X:
Dim su As StringUtils
Job.Poststring("https://example.com/wp-json/wc/v2/customers" , su.EncodeURL(JsonString, "UTF8"))

Thanks for the reply. But same issue. No improvement.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Here is an Java OkHttp example of calling a REST API: https://howtoprogram.xyz/2016/10/31/java-rest-client-examples-using-okhttp/

Going by that example I would remove
B4X:
Job.GetRequest.SetContentEncoding("text/plain")

I would modify
B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json")
to
B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json; charset=utf-8")

And I would not encode the JsonString variable and just use
B4X:
Job.PostString("https://example.com/wp-json/wc/v2/customers" , JsonString)
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Here is an Java OkHttp example of calling a REST API: https://howtoprogram.xyz/2016/10/31/java-rest-client-examples-using-okhttp/

Going by that example I would remove
B4X:
Job.GetRequest.SetContentEncoding("text/plain")

I would modify
B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json")
to
B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json; charset=utf-8")

And I would not encode the JsonString variable and just use
B4X:
Job.PostString("https://example.com/wp-json/wc/v2/customers" , JsonString)
Thanks for the effort. I will check and let you know.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Here is an Java OkHttp example of calling a REST API: https://howtoprogram.xyz/2016/10/31/java-rest-client-examples-using-okhttp/

Going by that example I would remove
B4X:
Job.GetRequest.SetContentEncoding("text/plain")

I would modify
B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json")
to
B4X:
Job.GetRequest.SetHeader("Content-Type", "application/json; charset=utf-8")

And I would not encode the JsonString variable and just use
B4X:
Job.PostString("https://example.com/wp-json/wc/v2/customers" , JsonString)


It didn't work. I tried here also http://onlinecurl.com/. the same error message is comming (400 Bad RequestInvalid request)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
yes. but how it could work in postmultipart. Also I found that postmultipart , post the request as bytes

For your additional information. the response is below

HTTP/1.1400 Bad Request
Date: Wed, 20 Sep 201713:23:44 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 140
Connection: keep-alive
Set-Cookie: PHPSESSID=fsi91l1e47hj44mem4dpct7np7; path=/
Expires: Wed, 11 Jan 198405:00:00 GMTCache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Robots-Tag: noindex
Link: <https://bcmcampus.000webhostapp.com/wp-json/>; rel="https://api.w.org/"
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Access-Control-Allow-Headers: Authorization, Content-Type
Allow: GET, POSTServer: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-ID: 28ccc627f4ea284a06d0b8f253eaafc8
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Your initial (first) post did not make it clear at all that your curl request did NOT work and therefore I tried to simulate your curl call as close as possible, which in the end was totally futile (and a waste of time). Looking through several sites, it looks like the WP API uses standard "application/x-www-form-urlencoded" POST requests, which is the equivalent to postmultipart. Another clue to that effect is this post on stackoveflow (https://stackoverflow.com/questions/36670944/wp-rest-api-v2-and-angular-http-post-request).
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
Your initial (first) post did not make it clear at all that your curl request did NOT work and therefore I tried to simulate your curl call as close as possible, which in the end was totally futile (and a waste of time). Looking through several sites, it looks like the WP API uses standard "application/x-www-form-urlencoded" POST requests, which is the equivalent to postmultipart. Another clue to that effect is this post on stackoveflow (https://stackoverflow.com/questions/36670944/wp-rest-api-v2-and-angular-http-post-request).

Sorry for the disappointment. Since I am a bigginer I couldn't get that.
I change the the contant type as 'Content-Type':'application/x-www-form-urlencoded'. But again it does not work.

Here I attached a sample project I tried. Button 1 uses "PostString" and Button 2 Uses "PostMultipart"

Edit :
I referred the following link for curl https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#create-a-customer

Error Code as stated above link
Error CodeError Type
400 Bad RequestInvalid request, e.g. using an unsupported HTTP method
401 UnauthorizedAuthentication or permission error, e.g. incorrect API keys
404 Not FoundRequests to resources that don’t exist or are missing
500 Internal Server ErrorServer error
 

Attachments

  • Curl.zip
    8.5 KB · Views: 289
Last edited:
Upvote 0
Top