West Wind Internet and Client Tools
Pass json to body of Post
Hello,
I need to pass a JSON to the body of the request. Here is sample code:
loHttp = newObject('wwHTTP', 'wwHTTP.prg')
I need to pass the following json to the post.
cJson = "[{"id": 19, "QtyStock": 100, "Price": 10, "Location":"MYTEST"}]"
This would be the post request URL:
cResponse = loHttp.httppost("192.168.1.167:9000/UpdateInStockQuantity")
How do I pass the json to the body of the request above?
TIA
Hi Dmitry,
Try
loHTTP.AddPostKey("somename",lcJson)
Carl
Hi Carl,
I have tried this with or without the first parameter (e.g. "somename") but the post does not work. The problem seems to be with the Content-type. If I set the loHTTP content type to "application/json", the call to the host has a result 500 (in Fiddler). If I do not set the content type and use HTTP default, the result is 200 but still nothing is posted.
Thank you for your suggestion.
Hi Dmitry,
Sorry, I wasn't thinking straight.
From the Help File (wwHTTP::cContentType)...
*** Send JSON data to a REST service
loHttp = CREATEOBJECT("wwHttp")
*** Specify the data sent is JSON
loHttp.cContentType = "application/json"
*** POST the raw JSON data
lcJSON = serializer.Serialize( loData )
loHttp.AddPostKey(lcJson)
lcResponse = loHttp.HttpGet(lcUrl)
Carl