Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbTargetCurl
    Handle hoHttpCurl
    Variant vResponseJson
    Handle hoResponseJson
    Integer iStatusCode
    String sTargetCurl
    String sTemp1

    Move False To iSuccess

    // 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.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbTargetCurl
    If (Not(IsComObjectCreated(hoSbTargetCurl))) Begin
        Send CreateComObject of hoSbTargetCurl
    End
    Get ComAppendLn Of hoSbTargetCurl " curl -X POST https://httpbin.org/post \" To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '      -H "Content-Type: application/json" \' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl "      -d '{" To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '            "title": "foo",' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '            "body": "bar",' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '            "userId": 1' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl "          }'" To iSuccess

    Get Create (RefClass(cComChilkatHttpCurl)) To hoHttpCurl
    If (Not(IsComObjectCreated(hoHttpCurl))) Begin
        Send CreateComObject of hoHttpCurl
    End

    // Run the curl command.
    Get ComGetAsString Of hoSbTargetCurl To sTemp1
    Get ComDoYourThing Of hoHttpCurl sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResponseJson
    If (Not(IsComObjectCreated(hoResponseJson))) Begin
        Send CreateComObject of hoResponseJson
    End
    Set ComEmitCompact Of hoResponseJson To False

    Get pvComObject of hoResponseJson to vResponseJson
    Get ComGetResponseJson Of hoHttpCurl vResponseJson To iSuccess

    Get ComStatusCode Of hoHttpCurl To iStatusCode
    Showln "response status code: " iStatusCode

    Get ComEmit Of hoResponseJson To sTemp1
    Showln sTemp1

    // 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"}'

    Move 'curl -X POST https://postman-echo.com/post -H "Content-Type: application/json" -d '{"foo":"bar"}'' To sTargetCurl

    // Run the curl command.
    Get ComDoYourThing Of hoHttpCurl sTargetCurl To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoResponseJson to vResponseJson
    Get ComGetResponseJson Of hoHttpCurl vResponseJson To iSuccess

    Get ComStatusCode Of hoHttpCurl To iStatusCode
    Showln "response status code: " iStatusCode

    Get ComEmit Of hoResponseJson To sTemp1
    Showln sTemp1

    // 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"
    // }


End_Procedure