Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.5.0+

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 B4X Downloads

B4X
Dim success As Boolean = False

'  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.
Dim sbTargetCurl As ChilkatStringBuilder
sbTargetCurl.Initialize
sbTargetCurl.AppendLn(" curl -X POST https://httpbin.org/post \")
sbTargetCurl.AppendLn("      -H " & Chr(34) & "Content-Type: application/json" & Chr(34) & " \")
sbTargetCurl.AppendLn("      -d '{")
sbTargetCurl.AppendLn("            " & Chr(34) & "title" & Chr(34) & ": " & Chr(34) & "foo" & Chr(34) & ",")
sbTargetCurl.AppendLn("            " & Chr(34) & "body" & Chr(34) & ": " & Chr(34) & "bar" & Chr(34) & ",")
sbTargetCurl.AppendLn("            " & Chr(34) & "userId" & Chr(34) & ": 1")
sbTargetCurl.AppendLn("          }'")

Dim httpCurl As ChilkatHttpCurl
httpCurl.Initialize("httpCurl")

'  Run the curl command.
success = httpCurl.DoYourThing(sbTargetCurl.GetAsString)
If success = False Then
    Log(httpCurl.LastErrorText)
    Return
End If


Dim responseJson As ChilkatJsonObject
responseJson.Initialize
responseJson.EmitCompact = False

httpCurl.GetResponseJson(responseJson)

Dim statusCode As Int = httpCurl.StatusCode
Log("response status code: " & statusCode)

Log(responseJson.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"}'

Dim targetCurl As String = "curl -X POST https://postman-echo.com/post -H " & Chr(34) & "Content-Type: application/json" & Chr(34) & " -d '{" & Chr(34) & "foo" & Chr(34) & ":" & Chr(34) & "bar" & Chr(34) & "}'"

'  Run the curl command.
success = httpCurl.DoYourThing(targetCurl)
If success = False Then
    Log(httpCurl.LastErrorText)
    Return
End If


httpCurl.GetResponseJson(responseJson)

statusCode = httpCurl.StatusCode
Log("response status code: " & statusCode)

Log(responseJson.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"
'  }