Visual FoxPro
Visual FoxPro
curl POST with JSON Input and JSON Output
See more CURL Examples
Demonstrates running a simple curl command with JSON input and JSON output.Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSbTargetCurl
LOCAL loHttpCurl
LOCAL loResponseJson
LOCAL lnStatusCode
LOCAL lcTargetCurl
lnSuccess = 0
* Run the following curl command
* curl -X POST https://httpbin.org/post \
* -H "Content-Type: application/json" \
* -d '{
* "title": "foo",
* "body": "bar",
* "userId": 1
* }'
* The backslashes at the end of lines are not required. Chilkat ignores them if present.
loSbTargetCurl = CreateObject('Chilkat.StringBuilder')
loSbTargetCurl.AppendLn(" curl -X POST https://httpbin.org/post \")
loSbTargetCurl.AppendLn(' -H "Content-Type: application/json" \')
loSbTargetCurl.AppendLn(" -d '{")
loSbTargetCurl.AppendLn(' "title": "foo",')
loSbTargetCurl.AppendLn(' "body": "bar",')
loSbTargetCurl.AppendLn(' "userId": 1')
loSbTargetCurl.AppendLn(" }'")
loHttpCurl = CreateObject('Chilkat.HttpCurl')
* Run the curl command.
lnSuccess = loHttpCurl.DoYourThing(loSbTargetCurl.GetAsString())
IF (lnSuccess = 0) THEN
? loHttpCurl.LastErrorText
RELEASE loSbTargetCurl
RELEASE loHttpCurl
CANCEL
ENDIF
loResponseJson = CreateObject('Chilkat.JsonObject')
loResponseJson.EmitCompact = 0
loHttpCurl.GetResponseJson(loResponseJson)
lnStatusCode = loHttpCurl.StatusCode
? "response status code: " + STR(lnStatusCode)
? loResponseJson.Emit()
* Output:
* response status code: 200
* {
* "args": {},
* "data": "{\r\n \"title\": \"foo\",\r\n \"body\": \"bar\",\r\n \"userId\": 1\r\n }",
* "files": {},
* "form": {},
* "headers": {
* "Content-Length": "96",
* "Content-Type": "application/json",
* "Host": "httpbin.org",
* "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968"
* },
* "json": {
* "body": "bar",
* "title": "foo",
* "userId": 1
* },
* "origin": "123.222.222.222",
* "url": "https://httpbin.org/post"
* }
* ----------------------------------------------------------------------------------
* Another example:
* curl -X POST https://postman-echo.com/post \
* -H "Content-Type: application/json" \
* -d '{"foo":"bar"}'
lcTargetCurl = 'curl -X POST https://postman-echo.com/post -H "Content-Type: application/json" -d '{"foo":"bar"}''
* Run the curl command.
lnSuccess = loHttpCurl.DoYourThing(lcTargetCurl)
IF (lnSuccess = 0) THEN
? loHttpCurl.LastErrorText
RELEASE loSbTargetCurl
RELEASE loHttpCurl
RELEASE loResponseJson
CANCEL
ENDIF
loHttpCurl.GetResponseJson(loResponseJson)
lnStatusCode = loHttpCurl.StatusCode
? "response status code: " + STR(lnStatusCode)
? loResponseJson.Emit()
* Output:
* response status code: 200
* {
* "args": {},
* "data": {
* "foo": "bar"
* },
* "files": {},
* "form": {},
* "headers": {
* "host": "postman-echo.com",
* "content-length": "13",
* "content-type": "application/json",
* "x-forwarded-proto": "https",
* "accept-encoding": "gzip, br"
* },
* "json": {
* "foo": "bar"
* },
* "url": "https://postman-echo.com/post"
* }
RELEASE loSbTargetCurl
RELEASE loHttpCurl
RELEASE loResponseJson