Http Request Header (Uploading Files)

I am trying to upload a file via HTTP request

My process before downloads files successfully, but I can’t work out how to get the files back up there once complete.

essentially I want to loop through a folder and upload the documents there.

Anyone else had success in this area?

Not sure how to replicate the following attributes in the body

image

Hi @barryrodick

I’ve already done something like this before…

I tried exhaustively using HTTP Client activity, and always I got HTTP success status but the file wasn’t submitted.

Then, I decided to implement in a different way, using invoke code activity.

Here you go the solution:

First, add RestSharp dependence to your project

After that, import RestSharp

Then, add an invoke code activity

image

Finally, add the following code (make changes as necessary):

    Dim client As RestClient = New RestClient("https://example.com/api/v2/uploads.json?filename=test.png")
    client.Timeout = 10000
    Dim request As RestRequest= New RestRequest(Method.POST)
    request.AddHeader("Content-Type", "image/png")
    request.AddParameter("image/png", File.ReadAllBytes("C:\Users\gustavo.cervelin\Downloads\test.png"), ParameterType.RequestBody)
    Dim response As IRestResponse = client.Execute(request)
    Console.WriteLine(response.Content)

IMPORTANT NOTES:

  1. At line five, set the content type according your file extension;
  2. The same for line six

That’s it!

If you need help to work with the response, let me know

1 Like

Hi @barryrodick ,
I needed to do something like that.
I send an image to a web service that decrypts what is written and gives me back a string (I use this to break captcha).

Using the HTTP Request activity (UiPath.Web.Activities) in the ** Attachments ** property, I configure the file to be sent.

The variable inCaminhoImagem contains the path of the file I want to send in the request, ex: c:\temp\imagem.png

I get HTTP Request: The given path’s format is not supported. error.

Also any idea how to pass the param in the body of ‘filename’

I imported restsharp, I thought it would be an option here

image

Dim client As RestClient = New RestClient("------/api/1/files/content?parentfolderid=973119")
client.Timeout = 10000
Dim request As RestRequest= New RestRequest(Method.POST)
request.AddHeader("Authorization","Bearer XXXXXXXXXXX")
request.AddHeader("Content-Type", "multipart/form-data;")
request.AddParameter("text/plain", File.ReadAllBytes("‪C:\Users\rodickb\Desktop\HiQTest.txt"), ParameterType.RequestBody)
request.AddParameter("filename","UploadTest1", ParameterType.RequestBody)
Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.Content

Ended your code above, but only given this error

Invoke code: Exception has been thrown by the target of an invocation.

@barryrodick,

In language select VBNet.

Is this API public or private?

If it is public, could you share its documentation?

And just to understand better…

Are you trying to upload two files in a single request or only one?

Thanks

sent the doc via PM.

Its a single doc

1 Like

Hi all,

Thanks to @gustavo.cervelin got the follow invoke code to work

Dim client As RestClient = New RestClient(“https://------------------/api/1/files/content?parentfolderid=973119”)
client.Timeout = -1
Dim request As RestRequest = New RestRequest(Method.POST)
request.AddHeader(“Authorization”, “Bearer -----------------”)
request.AddParameter(“filename”, “NameOfFile”)
request.AddFile(“file”, “C:/Users/rodickb/Desktop/HiQTest.txt”)
Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.Content)

1 Like

Glad to help you @barryrodick !

Wish you all the best :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.