cancel
Showing results for 
Search instead for 
Did you mean: 

Send Document using Multi Part Form Data in Web API

AmbrishSrivasta
Level 2
We are trying to consume a webservice which take a document and other metadata using multi part form data Content Type.
We are able to call the service successfully using Postman, but when using Web API feature of Blue Prism latest version 6.5 we are having following observations.
1. When using Template dropdown option in the Request, it is sending the whole data in json format instead of multi-part form data as expected by the service.
2. When using Single or Multi File option, we do not see a way to send additional attributes in the body as expected by service
3. We also wrote a custom code to send the information using multi-form, but even that ended up sending information in raw format.

Any experience on above requirement?

------------------------------
Ambrish Srivastava
Architect
Persistetnt Systems Inc
America/New_York
------------------------------
37 REPLIES 37

I'm using this technique, but keep getting a 500 error trying this.  The APIs documentation uses this curl format:

curl -X POST "https://exampleurl.com/platform-api/v1/documents" -H "accept: application/json" -H "Authorization: Bearer examplebearertoken" -H "Content-Type: multipart/form-data" -F "file=@CONSTARINT-732002.PDF;type=application/pdf" -F "document_name=CONSTARINT-732002.PDF" -F "project_id=40" -F "folder_path=" -F "resolve_conflicts="

My conversion is as follows;

-F
Content-Disposition: form-data; name="file"

[BinaryFile]; type="[filetype]"
-F
Content-Disposition: form-data; name="document_name"

[documentname]
-F
Content-Disposition: form-data; name="project_id"

[projectid]
-F
Content-Disposition: form-data; name="folder_path"

[folderpath]
-F
Content-Disposition: form-data; name="resolve_conflicts"

[resolve_conflicts]
-F

I think the issue is with the file type and location of the actual file variable, but can't figure out what the actual formatting would be.  In this case, it's possible that the file type might be application/pdf but could also be Word or other formats., which is why I'm using a variable.  Any tips?

Thanks in advance.

------------------------------
Kara Brooks
Senior Analyst
Seyfarth Shaw LLP
America/Chicago
------------------------------

What're you doing to try to get this to work?

To get it to work I ended up having to do a code state as shown below. I worked with the API endpoints for everything else and then created an object with multiple actions leveraging those endpoints and with one action for uploading files that used the code stage below.

var client = new RestClient("https://api.website.com/api/documents/"+documentID+"/file");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic "+token);
request.AddHeader("X-TR-API-APP-ID", API_Key);
request.AddFile("", File_Name);
IRestResponse response = client.Execute(request);
output=response.Content;


------------------------------
Tracy Schultz
------------------------------

Hi @Tracy Schultz,

I tried to use web api service to send binary data in multipart form as mentioned by Bimal.
But looks like binary data is getting corrupted. I couldn't debug the exact issue.
Can you please share me the complete ​code and configuration, where you have successfully uploaded the file.

------------------------------
Sheela Parthasarathy
Assistant Consultant
TCS
Pacific/Apia
------------------------------

Sheela,

 

Is it a form you are trying to upload? Also to truly help I'd have to know how this API works, without that information I'm a little limited in how I can assist. The code I posted was for the API I used but it should function similarly to the one you're using.

 

If you can provide more information on the API I will be able to assist you further.

 

  Tracy Schultz

 


DISCLAIMER: This message is confidential and may be privileged. If you are not an intended recipient, please notify the sender immediately by email or phone and permanently delete the message from your system before reviewing, copying, or distributing. You may call us at (800) 332-7952.


@Bimal Sebastian

I followed the template configuration as you suggested.
But I can see the [FileValue] - binary data is getting corrupted when I pass it as a parameter,

I created the API action with parameter FileValue of type Binary and added that parameter in the template.
Then in process, I converted the file into binary using ​LoadBinaryFile and passed that to uploadAPI action.
But In the request I can see the Binary data is corrupted.
Could you please let me know if there is any issue with my configuration with Binary data,


------------------------------
Sheela Parthasarathy
Assistant Consultant
TCS
Pacific/Apia
------------------------------

Hi Sheela,

Can you share some documentation of the destination URL. I mean any documentation that shows how the end point is expecting data.
I would also like to see a scren shot of the request section (specifically how you've configured the body)
Finally, can you show me a screen shot or something that shows what you mean by corruption

Thanks
Bimal

------------------------------
PS Support
PS Support Account for all of Professional Services
Blue Prism
Europe/London
------------------------------

I never did it using their API I did a code block as I posted. For me their API service template didn't work for uploading files as it wouldn't work with files. So I used the code I posted there and it takes the file path as a parameter and uses the Rest API to upload and convert the file through the C#.Net code block.

I configured everything else through the API definition, however I've found it best to build custom objects that leverage the Web API definitions and one of the actions that's part of this object is my custom file upload leveraging the API.

I've since had no issues and it works flawlessly. Give the code object I gave you a shot and see if that doesn't solve your issue and doesn't require you to jump through hoops trying to convert your file.

------------------------------
Tracy Schultz
------------------------------

Sheela,

it also depends on the target. I have worked recently with solution which expected a binary stream (PDF file) but when sending through Blue Prism Web API it was always base64 encoded string (captured with Fiddler). So I realized that I can't use Blue Prism Web API for sending binary file to this target but I created custom code in C# with RestSharp. See attachment. To make it work you need to download RestSharp https://www.nuget.org/packages/RestSharp.

Let me know if you need anything else.

Regards,

------------------------------
Zdeněk Kabátek
Head of Professional Services
NEOOPS
http://www.neoops.com/
Europe/Prague
------------------------------

Hi Tracy,

I am facing the similar issue, while trying to upload file(txt,pdf,excel,image etc), the file content is getting corrupted. I tried both template configuration and HTTP VBO as well. Could you please share the configuration required. Attaching the logs and input form (from api end) for reference.

26459.png
26460.png


------------------------------
Sireesha Jaggemsetti
Senior System Engineer
Infosys
Europe/London
------------------------------

@SireeshaJaggems

That doesn't do much good with out information on how the API expects things.

What does the API documentation state about how you're supposed to do the upload.
If you are trying to do as I did you need to use a code section and configure it to use C#.net.

I had to create my own Object with custom actions one of those being the upload file action. I did this for all the API endpoints as it allowed me to do more than just using the API action or setting them up in BluePrism. Does this make sense?

Look beyond what they provide, create your own objects with custom actions, you'll find this works better as you can make your work easier by doing this. I have better error handling, modify JSON on the fly and don't have to recreate these every single time.



------------------------------
Tracy Schultz
------------------------------