West Wind Internet and Client Tools
Pass json to body of Post
Gravatar is a globally recognized avatar based on your email address. Pass json to body of Post
  Dmitry Litvak
  All
  Jan 8, 2017 @ 01:17pm

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

Gravatar is a globally recognized avatar based on your email address. re: Pass json to body of Post
  Carl Chambers
  Dmitry Litvak
  Jan 9, 2017 @ 09:04am

Hi Dmitry,

Try

loHTTP.AddPostKey("somename",lcJson)

Carl

Gravatar is a globally recognized avatar based on your email address. re: Pass json to body of Post
  Dmitry Litvak
  Carl Chambers
  Jan 9, 2017 @ 09:12am

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.

Gravatar is a globally recognized avatar based on your email address. re: Pass json to body of Post
  Carl Chambers
  Dmitry Litvak
  Jan 9, 2017 @ 09:38am

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

© 1996-2024