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 ?
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.
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:
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
The hascode of AuthenticationHeaderValue can be generated by entering your username and password as show in the image below
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 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 .Can u please help.
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
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)