Skip to main content
Dec 16, 2022 3:43 PM UTC
Question

How to use POST multipart/form-data to upload a file

Hello,

We have a requirement to upload a file to an AWS endpoint using the POST method and multipart/form-data
We have the pieces in place and parameters added using addRequestParameter function, however the file is not getting uploaded with its contents. When we check the contents of the file uploaded in the server it contains the filename itself.

What are we missing?

*---------------
HTTP.POST.FILE:
*---------------

HTTP.METHOD="POST:multipart/form-data"
TIMEOUT=3600


svc_return_code = setHTTPDefault("VERSION", "1.1")

*
* Create security context
*
X.SECURE.HANDLE = ""
X.CONTEXT.RESULT = createSecurityContext(X.SECURE.HANDLE, "TLSv1.2")

*
* Create secure request to S3
*
svc_return_code = createSecureRequest(X.URL, HTTP.METHOD, REQUEST.HANDLE, X.SECURE.HANDLE)

*
* addRequestParameter
*

rc = addRequestParameter(REQUEST.HANDLE, 'key', X.KEY, '')
CRT 'rc key: ':rc

rc = addRequestParameter(REQUEST.HANDLE, 'AWSAccessKeyId', X.AWS.ACCESS.KEY, 'multipart/form-data')
CRT 'rc AWSAccessKeyId: ':rc

rc = addRequestParameter(REQUEST.HANDLE, 'x-amz-security-token', X.AMZ.TOKEN, 'multipart/form-data')
CRT 'rc x-amz-security-token: ':rc

rc = addRequestParameter(REQUEST.HANDLE, 'policy', X.POLICY, 'multipart/form-data')
CRT 'rc policy: ':rc

rc = addRequestParameter(REQUEST.HANDLE, 'signature', X.SIGNATURE, 'multipart/form-data')
CRT 'signature: ':rc

rc = addRequestParameter(REQUEST.HANDLE, 'file', 'D: empsample-import-big.csv', 'multipart/form-data')
CRT 'file: ':rc

svc_return_code = submitRequest(REQUEST.HANDLE, TIMEOUT, '', X.RESPONSE.HEADERS, X.RESPONSE.DATA, X.HTTP.STATUS)

    1 reply

    Dec 30, 2022 9:55 AM UTC
    Hello,

    We have a requirement to upload a file to an AWS endpoint using the POST method and multipart/form-data
    We have the pieces in place and parameters added using addRequestParameter function, however the file is not getting uploaded with its contents. When we check the contents of the file uploaded in the server it contains the filename itself.

    What are we missing?

    *---------------
    HTTP.POST.FILE:
    *---------------

    HTTP.METHOD="POST:multipart/form-data"
    TIMEOUT=3600


    svc_return_code = setHTTPDefault("VERSION", "1.1")

    *
    * Create security context
    *
    X.SECURE.HANDLE = ""
    X.CONTEXT.RESULT = createSecurityContext(X.SECURE.HANDLE, "TLSv1.2")

    *
    * Create secure request to S3
    *
    svc_return_code = createSecureRequest(X.URL, HTTP.METHOD, REQUEST.HANDLE, X.SECURE.HANDLE)

    *
    * addRequestParameter
    *

    rc = addRequestParameter(REQUEST.HANDLE, 'key', X.KEY, '')
    CRT 'rc key: ':rc

    rc = addRequestParameter(REQUEST.HANDLE, 'AWSAccessKeyId', X.AWS.ACCESS.KEY, 'multipart/form-data')
    CRT 'rc AWSAccessKeyId: ':rc

    rc = addRequestParameter(REQUEST.HANDLE, 'x-amz-security-token', X.AMZ.TOKEN, 'multipart/form-data')
    CRT 'rc x-amz-security-token: ':rc

    rc = addRequestParameter(REQUEST.HANDLE, 'policy', X.POLICY, 'multipart/form-data')
    CRT 'rc policy: ':rc

    rc = addRequestParameter(REQUEST.HANDLE, 'signature', X.SIGNATURE, 'multipart/form-data')
    CRT 'signature: ':rc

    rc = addRequestParameter(REQUEST.HANDLE, 'file', 'D: empsample-import-big.csv', 'multipart/form-data')
    CRT 'file: ':rc

    svc_return_code = submitRequest(REQUEST.HANDLE, TIMEOUT, '', X.RESPONSE.HEADERS, X.RESPONSE.DATA, X.HTTP.STATUS)

    Pedro,

    Try reading the csv file into a variable and replace the 'D: empsample-import-big.csv' with the variable. You are passing the text string of the file, not the file itself. 

    Jon