How to send a file attachment in the form-data parameter of the request to an API

I am trying to invoke an API which accepts request with form-data parameters, one of the parameter is a file , which can be of any type(.pdf/.jpg/.doc). When I try to pass the path of the file as value in parameters, I get an error from the API - the request was rejected because no multipart boundary was found.

When I try to send the file as an attachment , the file is not received in the expected format, instead it is received as application/octet-stream.

Request to please suggest options to send the file using the HTTP request acctivity.

Hello @Nagareetha_Baskaran!

It seems that you have trouble getting an answer to your question in the first 24 hours.
Let us give you a few hints and helpful links.

First, make sure you browsed through our Forum FAQ Beginner’s Guide. It will teach you what should be included in your topic.

You can check out some of our resources directly, see below:

  1. Always search first. It is the best way to quickly find your answer. Check out the image icon for that.
    Clicking the options button will let you set more specific topic search filters, i.e. only the ones with a solution.

  2. Topic that contains most common solutions with example project files can be found here.

  3. Read our official documentation where you can find a lot of information and instructions about each of our products:

  4. Watch the videos on our official YouTube channel for more visual tutorials.

  5. Meet us and our users on our Community Slack and ask your question there.

Hopefully this will let you easily find the solution/information you need. Once you have it, we would be happy if you could share your findings here and mark it as a solution. This will help other users find it in the future.

Thank you for helping us build our UiPath Community!

Cheers from your friendly
Forum_Staff

Hello Nagareetha,
In this video, you can see how I upload files to multiple systems (Maybe this will help you with your issue):
Uipath upload files in Jira (REST API) (HTTP REQUEST) | Jira REST API upload file - YouTube
ServiceNow Upload files without Administrator Rights with UiPath Robots | RPA files | REST API file - YouTube
Thanks,
Cristian Negulescu

Hello Nagareetha,
In this video, I use multipart/form-data:

0:20 cURL example
1:35 Send the command from POSTMAN
2:50 Implement the code in C#
4:20 Run the code
4:50 Visual Basic code VB.NET
6:20 New Process on UiPath Studio with Invoke Code
7:20 Imports Libraries in UiPath Studio
8:30 Run the Robot to Upload and Download the file

Blockquote
Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri(“YOUR URL”)
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(“application/vnd.openxmlformats-officedocument.wordprocessingml.document”))
// for BASIC AUTH
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(“username:password”)
client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue(“Basic”, Convert.ToBase64String(byteArray))Dim byteArray As Byte() = Encoding.ASCII.GetBytes(“username:password”)
// for OAUTH2
client.DefaultRequestHeaders.Add(“authorization”, “Bearer YOUR TOKEN”)
Dim request As MultipartFormDataContent = New MultipartFormDataContent()
request.Add(New StringContent(“tags”), “Bad”)
request.Add(New StringContent(“displayreferencetext”), “true”)
request.Add(New StringContent(“similaritythreshold”), “0.63”)
request.Add(New StreamContent(File.OpenRead(“C:\8A.docx”)), “file”, (New FileInfo(“C:\8A.docx”).Name))
Dim response As HttpResponseMessage = client.PostAsync(“”, request).Result
Dim strm As Stream = response.Content.ReadAsStreamAsync.Result
Dim doc As Byte()
Dim ms As MemoryStream = New MemoryStream()
strm.CopyTo(ms)
doc = ms.ToArray()
File.WriteAllBytes(“C:\rpa\FORDOWNLOAD\rez.docx”, doc)

Thanks,
Cristian Negulescu

Hi @Cristian_Negulescu ,

This code didn’t work, few of them are not valid, also I have installed the System.net.Http package and also imported that namespace too.

Hello,
Be sure you do all the steps from this timestamp of the movie:

Save and then restart the UiPath Studio.
As I Show in my movie the code was working for me.
Thanks,
Cristian

I have restarted the studio and also the system as well… also I have one more query , I have to pass one more item in body and it’s data is like

[{“name”: “ABC Field”, “values”: [{“value”: “UiPath”}]}]

How should I pass this value?

This is JSON you need to build like:

Okay will try again thank you.

If you don’t mind can you have a look to this topic, I am struggling to get this completed.

Hi,
I have checked and rectified the issue and fixed it, it was with the namespace, I was missing

System.Net.Http
But, on fixing it raise another concern I am keep getting 400 error whereas I had replicated the body inputs as I configured in Postman but over postman its working but in the UiPath I am keep getting the same 400 error, can you help? it’s a form-data POST method

Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri("https://xxx.xxx.com/v1/documents")

client.DefaultRequestHeaders.Add("authorization","Bearer XXX")
Dim request As MultipartFormDataContent = New MultipartFormDataContent()
request.Add(New StringContent("docId"),"CW1270984")
request.Add(New StreamContent(File.OpenRead(in_file)), "file", (New FileInfo(in_file).Name))

Dim response As HttpResponseMessage = client.PostAsync("", request).Result
Console.WriteLine(response)

Can you please help me rectify, why It giving this response?

This is the error I received as response

08/31/2022 18:15:48 => [Debug] StatusCode: 401, ReasonPhrase: ‘Unauthorized’, Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
CF-Ray: 7435dd85aae80e98-BOM
Strict-Transport-Security: max-age=2592000; includeSubDomains; preload
Vary: Cookie
Vary: Origin
CF-Cache-Status: DYNAMIC
evisort-trace-id: b1283fc0-83bc-4679-82ef-69de5f4e029f
referrer-policy: no-referrer-when-downgrade
x-content-type-options: nosniff
x-frame-options: DENY
alt-svc: h3=“:443”; ma=86400, h3-29=“:443”; ma=86400
Cache-Control: no-cache
Date: Wed, 31 Aug 2022 12:45:48 GMT
Server: cloudflare
WWW-Authenticate: Bearer realm=“api”
Content-Length: 31
Allow: POST
Allow: OPTIONS
Content-Type: application/json
Expires: Thu, 01 Jan 1970 00:00:01 GMT
}

I don’t know send Print Screen with Postman.