Upload de varios arquivos via API

Hello everyone, Could someone help me, I’m new and I have no idea how I would send files via API, would anyone have any idea how to do this.

ChatGPT output:

In UiPath, you can use the HTTP Request activity to send a file with an API. Here are the general steps to follow:

  1. Create an HTTP Request activity in your workflow, and set the HTTP method to POST or PUT, depending on the API’s requirements.
  2. Set the endpoint URL of the API and add any necessary headers.
  3. In the Content field, you can use the File.ReadAllBytes(path) method to read the file content and convert it to a byte array.
  4. Set the Content-Type header to the appropriate MIME type for the file.
  5. Set the Content-Disposition header to form-data and set the Name field to the key that the API is expecting.
  6. Set the FileName field to the name of the file.
  7. Execute the HTTP Request activity and get the response.

Here’s an example of how to send a file with an API in UiPath:

markdownCopy code

HTTP Request activity 
- Method: POST
- URL: https://api.example.com/upload
- Headers: 
  - Content-Type: application/octet-stream 
  - Content-Disposition: form-data; name="file"; filename="example.txt"
- Content: File.ReadAllBytes("C:\example.txt")

It’s important to note that the API you are using may have specific requirements for the format of the request, you should check the API documentation for more information.

Also, you can use the MultipartFormDataContent class from System.Net.Http namespace to set the headers and the file content more easily.

C#Copy code

var fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");

using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
    formData.Add(fileContent, "file", fileName);
    var response = await client.PostAsync(uri, formData);
    var responseString = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseString);
}

This way you can handle the file upload with more flexibility, and you can add other fields to the form-data if the API requires it.

Hi @Camilo_Cardoso

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