Sample code for 30+ languages & platforms
DataFlex

POST JSON to REST API with non-us-ascii Chars Escaped

See more REST Examples

Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBAutoReconnect
    Handle hoJson
    Variant vSb
    Handle hoSb
    Variant vSbResp
    Handle hoSbResp
    String sTemp1

    Move False To iSuccess

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    // Connect using TLS.
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "chilkatsoft.com" 443 True iBAutoReconnect To iSuccess

    // Load JSON containing the following Korean text.

    //  {
    //    "BillAddr": {
    //       "Id": "239615",
    //       "Line1": "류리하",
    //       "Line2": "류리하류리하",
    //       "City": "류리하류리하",
    //       "Country": "US",
    //       "CountrySubDivisionCode": "AK",
    //       "PostalCode": "류리하"
    //     }
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Set ComEmitCompact Of hoJson To False
    Get ComLoadFile Of hoJson "qa_data/json/korean.json" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAddHeader Of hoRest "Content-Type" "application/json; charset=UTF-8" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoSb to vSb
    Get ComEmitSb Of hoJson vSb To iSuccess
    Get ComEncode Of hoSb "unicodeescape" "utf-8" To iSuccess

    Get ComGetAsString Of hoSb To sTemp1
    Showln sTemp1

    // The StringBuilder contains this:

    // {
    //   "BillAddr": {
    //     "Id": "239615",
    //     "Line1": "\ub958\ub9ac\ud558",
    //     "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
    //     "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
    //     "Country": "US",
    //     "CountrySubDivisionCode": "AK",
    //     "PostalCode": "\ub958\ub9ac\ud558"
    //   }
    // }

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResp
    If (Not(IsComObjectCreated(hoSbResp))) Begin
        Send CreateComObject of hoSbResp
    End
    Get pvComObject of hoSb to vSb
    Get pvComObject of hoSbResp to vSbResp
    Get ComFullRequestSb Of hoRest "POST" "/echo_request_body.asp" vSb vSbResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Show the response. 
    Get ComGetAsString Of hoSbResp To sTemp1
    Showln "Json Response: " sTemp1


End_Procedure