I use this b4x code, the problem is the categories part, how can I past this in a map?
b4x code:
Sub Button1_Click
Dim D As Map
D.Initialize
D.Put("name","REST API TEST")
D.Put("type", "simple")
D.Put("regular_price", "21.99")
D.Put("description", "Long description")
D.Put("short_description", "Short description")
Dim A As Map
A.Initialize
A.Put("id", "9")
A.Put("id", "14")
D.Put("categories",A)
Dim Job As HttpJob
Job.Initialize("Register",Me)
Job.Username = "?????????????????"
Job.Password = "?????????????????????????????????"
Job.PostMultipart("https://???????????????????/wp-json/wc/v3/products" , D,Null)
Job.GetRequest.SetHeader("Content-Type", "application/json")
Job.GetRequest.SetContentEncoding("text/plain")
End Sub
I get the following error:
ResponseError. Reason: , Response: {"code":"rest_invalid_param","message":"Ongeldige parameter(s): categories","data":{"status":400,"params":{"categories":"categories[0] wrong type object."},"details":{"categories":{"code":"rest_invalid_type","message":"categories[0] wrong type object.","data":{"param":"categories[0]"}}}}}
The problem is the map A, but I can't find out the correct syntax.
Dim D As Map
D.Initialize
D.Put("name","REST API TEST")
D.Put("type", "simple")
D.Put("regular_price", "21.99")
D.Put("description", "Long description")
D.Put("short_description", "Short description")
Dim lst_Categories As List
lst_Categories.Initialize
lst_Categories.Add(CreateMap("id":9))
lst_Categories.Add(CreateMap("id":14))
D.Put("categories",lst_Categories)
'**********Testing********************
Dim json As JSONGenerator
json.Initialize(D)
Log(json.ToString)
9 is not the same as "9". see if a. stolte's code works when you change 9 and 14 to "9" and "14". or check with server to see what it is it's expecting.
I tried with 9 en "9", no difference.
Just poststring json results in creating new product with default values, not my values.
Postmultipart, without categories, functions OK!
@Alexander Stolte's code produces the JSON you requested and so can you point us to the REST API documentation so we can see if that reveals any clues?
Related to that, do you need to create the product categories first before you create a product with specified categories?
To me this would account for why you get no error when you omit categories from your post. It appears to me that you may have to first create the categories you want which will return the category id which you can use in subsequent product creations.
When you say "Just poststring json results in creating new product with default values, not my values":
what "default values" do you see for that new product?
did you literally post "json" or "json.ToString"?
Posting the actual code here for each of your test cases would be useful because sometimes what we think we do is not actually what the code does and extra sets of eyes can assist.
categories must be a "List" of "Maps" based on the json in #1
B4X:
Dim cl As List
cl.initialze
dim A as Map
A.Initialize
A.Put("id", "9")
cl.Add(A)
dim A as Map
A.Initialize
A.Put("id", "14")
cl.Add(A)
D.Put("categories",cl)
or simply
B4X:
Dim cl As List
cl.initialze
cl.Add(CreateMap("id":"9")
cl.Add(CreateMap("id":"14")
D.Put("categories",cl)
@Alexander Stolte's code produces the JSON you requested and so can you point us to the REST API documentation so we can see if that reveals any clues?
Related to that, do you need to create the product categories first before you create a product with specified categories?
To me this would account for why you get no error when you omit categories from your post. It appears to me that you may have to first create the categories you want which will return the category id which you can use in subsequent product creations.
When you say "Just poststring json results in creating new product with default values, not my values":
what "default values" do you see for that new product?
No description. short description, price, etc. Only "empty" article fields
did you literally post "json" or "json.ToString"?
I tried both!
Posting the actual code here for each of your test cases would be useful because sometimes what we think we do is not actually what the code does and extra sets of eyes can assist.
In visual basic code I use SetRequestHeader, I now only see GetRequestHeader.
Thanks for your comprehensive answers @ViMeAv ICT.
I thought your Sub Button1_Click code was correct in that you post json.ToString rather than the map directly as you do in Sub Button2_Click given no error is returned and so don't know why that doesn't work. My only thought was that
I've used B4X to access a variety of APIs, but I've not had experience with WooCommerce and so I created a free trial site to test code for myself. I found I needed to do the authentication differently because I was getting Unauthorised errors with the original method in your code. I got a hint from the documentation which said "Occasionally some servers may not parse the Authorization header correctly (if you see a "Consumer key is missing" error when authenticating over SSL, you have a server issue). In this case, you may provide the consumer key/secret as query string parameters instead."
So this code creates a new product with my own data and assigns it to a (existing) category successfully:
The replacement of the getrequest in button2_click fixed the job.
The button1_click with the map, instead of json, gives the error.
But no problem, I can use the button2_click option.
Strangely I can't get the PostMultipart version to work for my test site because when I include categories in the post it returns the error you reported in post #1: "categories[0] wrong type object." It seems that PostMultipart doesn't like a list within a map for me.
Yes the button1_click code using poststring won't work with the map because it requires a string, which in this case will be in JSON format (json.ToString).
Anyway, it has been an interesting mental exercise for me to learn about a new API.