Sample code for 30+ languages & platforms
DataFlex

Set a Multipart Part Body from a StringBuilder

See more REST Examples

Demonstrates Rest.SetMultipartBodySb, which sets the text body of the selected multipart part from a StringBuilder.

Background. A multipart request is assembled part by part: the part selector chooses the part being built, header fields describe it, and a SetMultipartBody method supplies its content. The request is then sent with a multipart send or full-request method.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vSbBody
    Handle hoSbBody
    String sResponseText
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Move True To iBTls
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "example.com" 443 iBTls iBAutoReconnect To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Select the multipart part to build, and set its header fields.
    Set ComPartSelector Of hoRest To "1"
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="field1"' To iSuccess
    Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess

    //  Set the text body of the selected part from a StringBuilder.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbBody
    If (Not(IsComObjectCreated(hoSbBody))) Begin
        Send CreateComObject of hoSbBody
    End
    Get ComAppend Of hoSbBody '{ "key": "value" }' To iSuccess
    Get pvComObject of hoSbBody to vSbBody
    Get ComSetMultipartBodySb Of hoRest vSbBody To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComFullRequestMultipart Of hoRest "POST" "/api/upload" To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sResponseText


End_Procedure