Posted: 13 Feb 2017 2:16 EST Last activity: 17 May 2017 5:22 EDT
How to pass a file as form data in connect rest post method body
There is a API I am connecting through connect rest post method. I require to browse and select a file from the local machine. This file requires to be passed in the body of the request while calling the connect rest post method.
While trying this out through any rest client (eg. postman) i see in body mentioned as form-data. This accepts a key and file as input. I am able to call the API and perform the functionality.
It seems that you want to send multipart data in the connect rest call
In general we have four properties to be set on pyRequestAttachmentPage(Data-ServiceMessage)
The generation of multipart request depends on the four properties which is present in Data-ServiceMessage class
pyAttachname,pyValues is mandatory to be set, pyAttachtype and pyAttachFilename
pyAttachtype generates content-type of the part, if not given falls back to default application/octetstream
pyAttachname generates the content-disposition header where name is the parameter
if pyAttachname option is not given the file name is defaulted to whatever value is specified in attachname
There are two types of "parts" that are supported: binary and text for the "multipart/form-data" content type.
For binary parts, all four parameters are required: name, filename, type and value.
For string parts, only the name and value are required, while the file name must be blank or null. The type may be specified but, if not, "text/plain" is assumed.
You can take a look at Pega-Int-Storage-Box.D_pxCreateFile, which calls the Pega-Int-Storage-Box.pzCreateFileRequest data transform. This data transform is used to upload an arbitrary file type in one part, along with JSON metadata in a string part.
We have implemented the same using multipart form data and type binary. We used the Pega default feature to add an attachment to work object. We created a button in actions tab configured event on click to perform the following in sequence, calling Local Action pyAttachContent, Set Value of .pyAttachmentType to "File", Calling an activity , Refreshing the harness.
In the activity we are opening the attachment through the key pyAttachments(n).pxLinkedRefTo property (Present in pyWorkPage) in a page of class Data-WorkAttach-File (SampleFile). Then creating a page pyRequestAttachmentPage of class Data-ServiceMessage and setting the four parameters
1)pyAttachNames(n) [This contains the parameter to be passed eg. name, file etc.];
2)pyAttachTypes(n) [This contains the content-type eg. application/json etc. ]. This should NOT be set to "multipart/formdata"
3)pyAttachValues(n) [This contains the binary data of the file to be passed] the value is SampleFile.pyAttachStream,
4)pyAttachFileNames(n) [This contains the entire file name with extension] the value is SampleFile.pxAttachName
We call the Connect rest with this page in clipboard and the file gets send along with the request.
In our case we did not want the file to be in work object so deleted the attachment from class Link-Attachment (pyWorkPage.pyAttachments(n).pzInsKey ) and Data-WorkAttach-File (pyWorkPage.pyAttachments(n).pxLinkedRefTo).Then refreshed the data page D_AttachmentList passing the parameter as Work Object pzInsKey.
I also have the same requirement. Have to pass file to Connect-Rest rule. Though I set values in pyRequestAttachmentPage, the json request doesnt show the file. I am getting file required error. Could you please help.
Please find the documents attached.
This comment has been branched out as a new node and the new thread can be followed here