Sample code for 30+ languages & platforms
DataFlex

curl with Variable Substitution in a GraphQL Request Body

See more CURL Examples

This example shows how to use variables inside a graphql request body using the {{variable_name}} syntax. When the HTTP request’s Content-Type indicates graphql, Chilkat automatically applies proper escaping to each substituted value, ensuring the resulting graphql remains valid.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbCurl
    Handle hoCurl
    Variant vSbRawRequest
    Handle hoSbRawRequest
    String sTemp1

    Move False To iSuccess

    Move False To iSuccess

    // Variable names are enclosed between {{ and }}
    // Important: Variables should be placed inside the quotes.

    // curl -X POST https://api.example.com/graphql \
    //   -H "Content-Type: application/graphql; charset=utf-8" \
    //   -H "Accept: application/json" \
    //   --data-binary "mutation {
    //   createUser(
    //     input: {
    //       name: \"{{name}}\"
    //       city: \"{{city}}\"
    //       note: \"{{note}}\"
    //       bio: \"{{bio}}\"
    //     }
    //   ) {
    //     id
    //     name
    //   }
    // }"

    // Build the above curl command.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbCurl
    If (Not(IsComObjectCreated(hoSbCurl))) Begin
        Send CreateComObject of hoSbCurl
    End
    Get ComAppendLn Of hoSbCurl "curl -X POST https://api.example.com/graphql \" To iSuccess
    Get ComAppendLn Of hoSbCurl '  -H "Content-Type: application/graphql; charset=utf-8" \' To iSuccess
    Get ComAppendLn Of hoSbCurl '  -H "Accept: application/json" \' To iSuccess
    Get ComAppendLn Of hoSbCurl '  -d "mutation {' To iSuccess
    Get ComAppendLn Of hoSbCurl "  createUser(" To iSuccess
    Get ComAppendLn Of hoSbCurl "    input: {" To iSuccess
    Get ComAppendLn Of hoSbCurl '      name: \"{{name}}\"' To iSuccess
    Get ComAppendLn Of hoSbCurl '      city: \"{{city}}\"' To iSuccess
    Get ComAppendLn Of hoSbCurl '      note: \"{{note}}\"' To iSuccess
    Get ComAppendLn Of hoSbCurl '      bio: \"{{bio}}\"' To iSuccess
    Get ComAppendLn Of hoSbCurl "    }" To iSuccess
    Get ComAppendLn Of hoSbCurl "  ) {" To iSuccess
    Get ComAppendLn Of hoSbCurl "    id" To iSuccess
    Get ComAppendLn Of hoSbCurl "    name" To iSuccess
    Get ComAppendLn Of hoSbCurl "  }" To iSuccess
    Get ComAppendLn Of hoSbCurl '}"' To iSuccess

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

    // Provide values for variables
    Send ComSetVar To hoCurl "name" "José O'Connor"
    Send ComSetVar To hoCurl "city" "München"
    Send ComSetVar To hoCurl "note" 'He said "Hello, world!" — and left…'
    Send ComSetVar To hoCurl "bio" "Loves sushi, café visits, and π ≈ 3.14159"

    // To demonstrate how the variables are replaced, this example does not execute the curl command. 
    // Instead, it generates the raw HTTP request that would be sent if the curl command were run.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRawRequest
    If (Not(IsComObjectCreated(hoSbRawRequest))) Begin
        Send CreateComObject of hoSbRawRequest
    End
    Get ComGetAsString Of hoSbCurl To sTemp1
    Get pvComObject of hoSbRawRequest to vSbRawRequest
    Get ComToRawRequest Of hoCurl sTemp1 vSbRawRequest To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbRawRequest To sTemp1
    Showln sTemp1

    // The output is shown below.
    // Notice the quote chars around "Hello, world!" are properly escaped.

    // POST /graphql HTTP/1.1
    // Accept: application/json
    // Host: api.example.com
    // Content-Type: application/graphql; charset=utf-8
    // Content-Length: 250
    // 
    // mutation {
    //   createUser(
    //     input: {
    //       name: "José O'Connor"
    //       city: "München"
    //       note: "He said \"Hello, world!\" — and left…"
    //       bio: "Loves sushi, café visits, and π ≈ 3.14159"
    //     }
    //   ) {
    //     id
    //     name
    //   }
    // }


End_Procedure