Sample code for 30+ languages & platforms
DataFlex

REST File Upload (multipart/form-data)

See more REST Examples

Demonstrates how to upload a file using multipart/form-data.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Variant vFileStream
    Handle hoFileStream
    String sResponseBody
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Connect to the HTTP server using TLS.
    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    // Make sure to replace "www.chilkatsoft.com" with your domain..
    Get ComConnect Of hoRest "www.chilkatsoft.com" iPort iBTls iBAutoReconnect To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStream)) To hoFileStream
    If (Not(IsComObjectCreated(hoFileStream))) Begin
        Send CreateComObject of hoFileStream
    End
    Set ComSourceFile Of hoFileStream To "qa_data/jpg/starfish.jpg"

    Get ComAddHeader Of hoRest "Content-Type" "multipart/form-data" To iSuccess

    Set ComPartSelector Of hoRest To "1"
    Get ComAddHeader Of hoRest "Content-Type" "image/jpg" To iSuccess
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="filename"; filename="starfish.jpg"' To iSuccess
    Get pvComObject of hoFileStream to vFileStream
    Get ComSetMultipartBodyStream Of hoRest vFileStream To iSuccess
    Set ComPartSelector Of hoRest To "0"

    Get ComFullRequestMultipart Of hoRest "POST" "/the_uri_path_to_receive_the_upload" To sResponseBody
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "Received error response code: " iTemp1
        Procedure_Return
    End

    Showln "File uploaded"


End_Procedure