http POST of JSON data to remote server

Posted By: SBGuy

http POST of JSON data to remote server - 06/08/20 16:08

I'm trying to post a simple JSON to a remote server. The command line curl test works, but I can't seem to properly format my JSON string in the data variable for an http_post or http_send call. Any idea where my mistake is? I've tried both single quote and escaped double quotes.

Thanks!

Code
	string data = "{\
		\"apikey\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\", \
		\"systemid\":\"129392723\", \
		\"signal\":{ \
			\"action\":\"BTO\", \
			\"quant\": 3, \
			\"symbol\":\"AAPL\", \
			\"typeofsymbol\":\"stock\", \
			\"market\": 1, \
			\"duration\":\"DAY\" \
		} \
	}";

	int id = http_post(URL, data);



*************** server response *********************

{
   "ok" : "0",
   "error" : {
      "title" : "Invalid JSON",
      "message" : "\"':' expected, at character offset 1 (before \\\"\\\\\\\\r\\\\n\\\\t\\\\t\\\"apikey\\\":\\\"6...\\\")\\n\""
   }
}

Posted By: SBGuy

Re: http POST of JSON data to remote server - 06/08/20 16:42

Solved!!! Apparently the line continuation backslash is the problem.

I supposed if I used some json library I wouldn't have this problem, but it was a simple POST so I didn't bother.
Posted By: onoff

Re: http POST of JSON data to remote server - 06/08/20 17:38

I highly recommend Postman for building and testing remote calls to APIs and whatnot...
Posted By: AndrewAMD

Re: http POST of JSON data to remote server - 06/08/20 17:47

Originally Posted by SBGuy
I supposed if I used some json library I wouldn't have this problem, but it was a simple POST so I didn't bother.
Generating json from scratch is easy. It's parsing json which is a bit more tedious and might justify the use of a library.

In my blog, I use jsmn as a library to parse json in Zorro:
https://www.vitaltrades.com/2019/01...sing-zorro-script-part-2-parse-any-json/

You also have the option of loading a json file directly so that you don't have to intermix C and json so much.
© 2024 lite-C Forums