Inbound Multipart/form-data via the Scripted REST API (in a scoped application)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2019 02:31 PM
Is it possible to accept multipart/form data via the Scripted REST API (in a scoped application)? A vendor can only send their data and associated attachments using multipart/form-data and they also are unable to utilize any ServiceNow information (like a sys_id) when sending the submission. Any time I try to POST to the Scripted REST API with a multipart/form-data Content-Type, I am currently receiving an error message stating "com.glide.rest.domain.ServiceException: Service error: attempt to access request body is not allowed when request method is GET or DELETE".
Has anyone successfully parsed an inbound multipart/form data via the Scripted REST API (in a scoped application)?
===============
UPDATE:
Let me add some additional details as it appears the (much appreciated) responses are getting off track.
- Both the Scripted REST Service and the associated Scripted REST Resource have 'multipart/form-data' added as a value added under the 'Default supported request formats' (with 'Override default supported request formats' checked as TRUE).
- The Scripted REST Resource has the 'HTTP Method' set as 'POST'
The multipart/form-data contains multiple parts (which are separated by the boundary which is defined in the header), including base64 encoded attachments, binary attachments, and application/json data to be processed. I will need to use data from the application/json part of the payload to determine if this will be creating a new target record or updating an existing record (a coalesce value is within the JSON). The remaining parts of the payload will be attachments on the target record (and I know how to create the base64 encoded data as an attachment and have a plan on how to create the attachments from the binary data). However, I can't even get to start parsing any of the payload if the system won't even allow me to access the request body (based on the error message).
Essentially, the system seems to be providing an invalid error when I am attempting to process a POST request with a multipart/form-data Content-Type, somehow interpreting the submission instead as a GET or DELETE method rather than a POST.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2024 04:59 PM
I know this is a pretty old thread, but I have finally found a working solution for this. It is far from perfect and should rather be considered a "proof of concept".
Knowledge I gained while tinkering:
- request.body.dataStream must be used (dataString is not supported and access to it will crash)
- copy the whole request body into a temporary attachment (using the GlideSysAttachment stream API)
- parse this temporary attachment using the the global (semi documented) GlideSysAttachmentInputStream and the ByteArrayOutputStream (Packages ...) API
- parsing the Form Data needs to handle text based and binary based data, split by the multipart "boundary" value
Here is a working example for a multipart (POST) endpoint:
https://github.com/kr4uzi/ServiceNow-Public-Record-Producer-Attachments/blob/main/fb6ef9ba97c7f1103c...
Note: The GlideScopedEvaulator is used to execute some global APIs in this scoped application. Most of the application can be used 1:1 in global scope.