Search code examples
azureazure-data-factory

How to send form-data in a POST body using ADF Web Activity


I recently worked on a task where I have to extract data from API using ADF. The API uses bearer token for authentication and to generate the token it uses form-data. The api require Username and Password as Body form-data in a POST web activity request as shown below in postman UI enter image description here

I didn't get much information how to pass the form-data using ADF web activity, so thought of giving the solution which worked for me and might be useful to other.


Solution

  • The link Content-Disposition provide the information about the syntax and format to send the form-data.

    Below is an HTML form example from the link and based on this I created the format of the body request for ADF

    POST /test.html HTTP/1.1
    Host: example.org
    Content-Type: multipart/form-data;boundary="boundary"
    
    --boundary
    Content-Disposition: form-data; name="field1"
    
    value1
    --boundary
    Content-Disposition: form-data; name="field2"; filename="example.txt"
    
    value2
    --boundary--
    

    For ADF POST web activity we require the headers and body. This is multipart form data, and because the body can contain many values, you need a boundary that's used to separate them.

    To set boundary I generated the GUID using NEWID() and removed the hyphen(-) enter image description here

    Set the below header require for multipart form data along with other headers require for API.

    multipart/form-data; boundary=B0769B09344440F5B6373793F80F87F8
    

    enter image description here

    Set the body with the boundary passed in header and be careful with the "\n" or "\r" in the body structure. Some API supports both in my case it supports line feed only"\n". Also notes the usage of 2 hyphens(--) in the boundary , especially the last boundary where it start and end with (--). I used 2 variables varUsername and varPassword to pass the value of Username and Password.

    @concat('
    --B0769B09344440F5B6373793F80F87F8
    Content-Disposition: form-data; name="username"
    Content-Type: text/csv
    
    ',variables('varUserName'),'
    --B0769B09344440F5B6373793F80F87F8
    Content-Disposition: form-data; name="password"
    Content-Type: text/csv
    
    ',variables('varPassword'),'
    --B0769B09344440F5B6373793F80F87F8--
    ')
    

    enter image description here

    ADF JSON code looks like this for body expression

    "body": {
                            "value": "@concat('\n--B0769B09344440F5B6373793F80F87F8\nContent-Disposition: form-data; name=\"username\"\nContent-Type: text/csv\n\n',variables('varUserName'),'\n--B0769B09344440F5B6373793F80F87F8\nContent-Disposition: form-data; name=\"password\"\nContent-Type: text/csv\n\n',variables('varPassword'),'\n--B0769B09344440F5B6373793F80F87F8--\n')",
                            "type": "Expression"
                        }
    

    I hope this might be useful for someone.

    Thanks