Unable to Http Request POST API With Attachment

Hi Team,

I am trying to call the POST API by Attaching the two attachments (PDF and JSON). But it is working fine in CMD using CURL but when i am trying it is giving some error.

curl -X POST http://localhost:3001/api/v1/document -H “Content-Type:multipart/form-data” -F “file=@CSE.pdf;type=application/pdf” -F “config=@D:\Parsr\Parsr-0.11.2\Parsr-0.11.2\server\defaultConfig.json;type=application/json”

Can any help me on above command how can we implement it in UiPath with HTTP Request ?

1 Like

Hi Shashank,

Following is the curl integration in the UiPath, where I have an email gateway (as similar to sms gateway) to trigger the emails with the Curl Command. Note this example uses multipart form data. (As a form-data). I am not sure about http request activity but I hope this might help you.

The request looks like this in Postman:

UiPath Integration:

Install Packages:
Microsft ASP.Net Web Api 2.2 Client Library
System.Net.Http

Import following from import panel
System
System.Net.Http
System.Net.Http.Header

Use Invoke Code Activity with the following VB.Net code

Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri(“Mention your BaseURL”)
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(“application/json”))
client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue(“Basic”, “your hash code”)
Dim request As MultipartFormDataContent= New MultipartFormDataContent()
request.Add(New StringContent(“John@domain.com”), “from”)
request.Add(New StringContent(“to@domain.com”), “to”)
request.Add(New StringContent(“Mail subject”), “subject”)
request.Add(New StringContent(“Mail Body”), “text”)
request.Add(New StreamContent(File.OpenRead(“C:/Users/Desktop/filename.pdf”)), “attachment”, (New FileInfo(“C:/Users/Desktop/filename.pdf”).Name))
Dim response As HttpResponseMessage = client.PostAsync(“email/2/send”, request).Result
If response.IsSuccessStatusCode Then
Dim responseContent As HttpContent= response.Content
Dim responseString As String = responseContent.ReadAsStringAsync.Result
Console.WriteLine(responseString)
Else
Console.WriteLine(("failed: " + response.Content.ReadAsStringAsync.Result))
End If

Note:

  1. BaseUrl is the text before email/2/send ie. if your api end point is https:/abcde.api.gatway.com/email/2/send then https:/abcde.api.gatway.com/ is your baseURL

  2. The hascode of AuthenticationHeaderValue can be generated by entering your username and password as show in the image below

Hi @Vishal_K,

If i were to have a specific type for the attachment - see below screenshot, how can i input that into the code?

Much appreciated.
Regards,

Jordan C

Hello Jordan,
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

2 Likes

Hey Negulesscu,
Thanks a lot .It worked .Can u please tell how to see the response from the api .I have used invoke code(ui path)

Hello Tharani,
To see the response code you have these properties on the response variable:
response.StatusCode
response.IsSuccessStatusCode

The values for StatusCode are here:

To return the value in UiPath you need an output argument on the invoke code.
Thanks,
Cristian Negulescu

1 Like

Thanks a lot!!! it worked like a champ. I need to pass the image as attachment .I need to convert image into byte array .Tried importing System.Drawing in uipath .But getting error as image .Can u please help.

Hello Tharani,
If you write like this will work

Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(“d:\bank-copy.png”)
Using mStream As New MemoryStream()
img.Save(mStream, img.RawFormat)
mStream.ToArray()
End Using

Thanks for the reply Cristian .getting this error image

Hi, I am keep getting error on the below code

Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri(in_API)

client.DefaultRequestHeaders.Add(“authorization”, in_Authorization)
Dim request As MultipartFormDataContent = New MultipartFormDataContent()
request.Add(New StringContent(in_docID), “docId”)
request.Add(New StringContent(in_fields), “fields”)
request.Add(New StreamContent(File.OpenRead(in_file)), “file”, (New FileInfo(in_file).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)