I have a problem making a POST call to “Contentful” API for uploading an image and I am hoping that you can point me in the right direction. I searched the forum and found the following topic which seems to be pretty close to what I want to do, so I tried to adapt this to my case.
The call I am trying to perform looks like this and the image needs to be sent in binary format.
Please find the steps I my workflow and let me know if you notice anything wrong.
I set a variable (InputFile) with the full path to my image file
Then I read the bytes using another assignment activity - FileBytes = File.ReadAllBytes(InputFile)
As the HTTP Request body does not accept an Array of Bytes (Bytes) I convert the “FileBytes” to Base64String - FileBitString = Convert.ToBase64String(FileBytes).
Just to confirm that the bytes are read correctly I set the following VB Code in Invoke Code to generate the image at another location - File.WriteAllBytes(OutputFile, Convert.FromBase64String(FileBase64String)). I can confirm that the conversion works as expected.
As an additional step I am also outputting the Base64String to a .“bin” file as I tried to attach it to the HTTP Request action, but this should not be necessary.
This is what my HTTP Request looks like where I passed the Base64String variable in the Body and changed the format to “application/binary”.
You can explore the details what is done by POSTMAN when checking the POSTMAN Console
With this output it can be rated if the call can be done with the HTTP Request activity (due there can be some limitations which will restrict the usage)
As an alternate HTTPClient custom call can be checked as it can be an alternate
Unfortunately, I have limited permissions on my work machine and cannot install POSTMAN, but will look for some alternative.
In the meantime, would you be able to provide me with an example of how the HTTPClint can be used in UiPath to send a file via an API call as this is not something I have ever tried/used before? Thanks
It depends on the API you are consuming. In previous experiences I’ve uploaded files using the File Attachments (Multipart Form Data requests) or by including the encoded content of the file as a field in a json body. This case is another option. Let me try it and I’ll get back to you with more clarity
I have tried to attach the image file path as data-binary and also to pass the base64 string in the body, but it didn’t work. I also tried to remove the body completely and just use the attached image as I read that Postman automatically converts images to BASE64 if the “form-data” option is used in the body.
However, if I remove the attached image it just returns “The resource is not found” as per the screenshot below:
I have performed several GET calls so I am sure that my space and environment IDs are correct. I also tested uploading an image through the GUI and it works fine, so I am not sure if there is anything that needs to be enabled on the Contentful side so I can use the upload API?
I also tried to retrieve an asset using the below call, but it can’t find the file I am looking for:
I took the asset ID through the Contentful GUI > Media > Pick and Item > Info section.
Would you be able to check if this works for you? If it does then the problem should be with my account as I also tested the calls through PowerShell and the results are exactly the same.