API and HTTP Request: Want to post image(png format) via API call

Hi ,

Want to post image via API and request is giving 201 as well but it’s showing “It looks like we don’t support this file format” while opening the image.

please find details filled in http request:
body: application/json
content-type: image/png
file path in http request {Options - Attachment section}

not sure where I am gng wrong.
FYI, it’s for Service Now APIs.
in postman when file is being uploaded in binary format it works.

Maybe you can Base64 encode the image and send as binary?

1 Like

@anjasing

Are you sending the file as base64 in body using postman?

If so then we ahould follow the same here as well

Cheers

in my old company i had the same problem as you,
this is how i did it
step 1. i used UiPath codeflow
step 2. used RestSharp
step 3. convert image to base64

1 Like

in SNOW APIs we are asked to not convert the image into base64 and directly pass the file path.
Also, we didn’t convert the file into base64 in postman. We just used body as binary and passed the file there

Can you please help @pikorpa . Have seen your one thread on this?

Also, after conversion base64 where can we exactly pass this?

Could u please check HTTP request all fields?

You can use powershell to call api, use PowerShell ISE to test first

@anjasing

  1. Did you try integration services? you have direct activities for these
  2. Also here also then you need to pass in binary in body only as postman internally does the conversion

also check this …a detailed explanation is here and for snow only

cheers

HI @Anil_G ,

Can you please check these fields if these are correct?

I’ve tried binary as well. It’s not working.

@anjasing

If you can give the postman collection…if it is ok…please share …even personal message if you dont want to share here…I can check from here on how exactly it should be

Cheers

Try
’ Define ServiceNow instance details
Dim serviceNowUrl As String = in_UploadAttachEndpointURL
Dim bearerToken As String = in_bearerToken
’ The sys_id of the incident to attach the image to
Dim incidentSysId As String = in_incidentNumberSysID
’ The file path of the image to be uploaded
Dim filePath As String = in_filePath
’ Convert the image to Base64
Dim fileBytes As Byte() = System.IO.File.ReadAllBytes(filePath)
’ Dim base64File As String = Convert.ToBase64String(fileBytes)
’ Upload the image
Using client As New system.Net.Http.HttpClient()
’ Set Bearer Token Authorization header
client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue(“Bearer”, bearerToken)
’ Prepare the JSON payload
’ Prepare the file content To be uploaded
Using fileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim content As New MultipartFormDataContent()
’ Create StreamContent for the file
Dim fileContent As New StreamContent(fileStream)
fileContent.Headers.ContentType = New MediaTypeHeaderValue(“image/png”) ’ Change to ‘image/png’ if needed
’ Add content to the multipart form data
content.Add(fileContent, “file”, Path.GetFileName(filePath))
content.Add(New StringContent(incidentSysId), “table_sys_id”)
content.Add(New StringContent(“incident”), “table_name”)
’ Make the POST request
Dim response As HttpResponseMessage = client.PostAsync(serviceNowUrl, content).Result
out_Response = CInt(response.StatusCode)
out_ResponseMessage = response.ReasonPhrase
End Using
End Using
Catch ex As Exception
out_Exception = ex.message
End Try

Using this invoke code activity. Can anyone help with it as it’s giving bad request and 400.