Send files to OpenAI via HTTP Request

Hi there!

I´ve trying to send files to OpenAI so it can make me a resume of them all. So far, this is what I created:

This is the endpoint: https://api.openai.com/v1/files
The Api Key is configured.
The local file points to an accesible and readable pdf file.

vListFormDataPart = New List(Of UiPath.Web.Activities.Http.Models.FormDataPart) From {New UiPath.Web.Activities.Http.Models.FileFormDataPart(“{pdf filepath}”, “file”, “application/pdf”), New UiPath.Web.Activities.Http.Models.TextFormDataPart(“purpose”, “answers”)}

The response:
“StatusCode”: “BadRequest”,
“TextContent”: "
{
\n "error":
{
\n "message": "Additional properties are not allowed (‘2025-001120179.pdf’,
‘answers’ were unexpected)",\n "type": "invalid_request_error",\n "param": null,\n "code": null\n
}\n
}\n",
“BinaryContent”: “”,
“File”: null,

Hi @Alejandro_alcantara

As per my understanding of your request, This will help to upload the file.

You can use this Curl with post request & replace the file path & add API key.

curl https://api.openai.com/v1/files ^
-H “Authorization: Bearer YOUR_OPENAI_API_KEY” ^
-F “purpose=assistants” ^
-F “file=@C:\Path\To\Your\File.pdf;type=application/pdf”

Also you are trying to upload the file with purpose = answers
that will not work with new api, Valid are assistants, fine-tune, batch

can you elaborate your use case in full so that we can able to help.

Hi,

With this code:
’ Inputs:
’ vStrApiKey As String → API Key OpenAI
’ vStrArchivo As String → FilePath
’ vStrPurpose As String → “assistants”
’ in_Config(“API_ENDPOINT_SUBIDA”) → URL API

’ Output:
’ vStrResponse As String → JSON response

Dim client As New System.Net.Http.HttpClient()
Dim requestContent As New System.Net.Http.MultipartFormDataContent()

’ Add file
Dim fileBytes() As Byte = System.IO.File.ReadAllBytes(vStrArchivo)
Dim fileContent As New System.Net.Http.ByteArrayContent(fileBytes)
fileContent.Headers.ContentType = New System.Net.Http.Headers.MediaTypeHeaderValue(“application/octet-stream”)
requestContent.Add(fileContent, “file”, System.IO.Path.GetFileName(vStrArchivo))

’ Add purpose
Dim purposeContent As New System.Net.Http.StringContent(vStrPurpose)
requestContent.Add(purposeContent, “purpose”)

’ API Key
client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue(“Bearer”, vStrApiKey)

’ Send POST request
Dim response As System.Net.Http.HttpResponseMessage = client.PostAsync(in_Config(“API_ENDPOINT_SUBIDA”).ToString, requestContent).Result

’ Response
vStrResponse = response.Content.ReadAsStringAsync().Result

I managed to succesfully send the file to OpenAI.
Thanks for the help.

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