Can I forward inbound api data stream to another endpoint?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 04:52 AM
Hello.
I am creating scripted rest api with multipart/form-data content-type,
and I found that below API document that datastream can be forwarded to another endpoint.
So I tried to data stream of requset body to attachment API(api/now/attachment/upload) without any change.
But it did not work.
'Failed to create the attachment. File part might be missing in the request.' message appeared.
I think form-data text in data stream body may be the reason, but I don't know how to delete text part in data stream.
Below is my code and data stream payload,
can anyone help me to solve this?
# Code
var reqStream = request.body.dataStream;
var reqAtt = new sn_ws.RESTMessageV2();
reqAtt.setRequestBody(reqStream);
reqAtt.setEndpoint(gs.getProperty('glide.servlet.uri') + 'api/now/attachment/upload');
reqAtt.setHttpMethod('POST');
reqAtt.setBasicAuth('*****', '***************');
reqAtt.setRequestHeader("Accept", "application/json");
reqAtt.setRequestHeader('Content-Type', 'multipart/form-data;boundary=--991805611325807099642862');
reqAtt.setRequestBody(reqStream);
var responseAtt = reqAtt.execute();
# Request data stream
----991805611325807099642862
Content-Disposition: form-data; name="table_name"
incident
----991805611325807099642862
Content-Disposition: form-data; name="table_sys_id"
e415794f1bc4a5d01ecfa864604bcbad
----991805611325807099642862
Content-Disposition: form-data; name="file"; filename="image_test.jpg"
Content-Type: image/jpeg
�����
----991805611325807099642862--
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2023 11:37 AM
Unfortunately setRequestBody() is not correct way for your case as reqStream is not a string but object GlideScriptableInputStream. You can try with method setRequestBodyFromAttachment which is more work to do as you need to prepare proper attachment in correct multipart pattern but only whis allows to send proper multipart/form request using class sn_ws.RESTMessageV2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 04:40 AM
you can access content of multipart request by request.body.dataStream - this is object you can use to save as attachment. It is not file in multipart rest message, but whole multipart content with boundary ect. You need some attachment host where you can save it and use later on
var attachmentID = new GlideSysAttachment().writeContentStream(
grAttachmentHost,
'multipart_request.txt',
'text/plain',
request.body.dataStream);
you can use this multipart content to forward it to OOTB api easly once you have proper multipart content in attachment
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod('POST');
restMessage.setEndpoint(gs.getProperty('glide.servlet.uri') + 'api/now/attachment/upload');
restMessage.setBasicAuth('user', 'pass');
restMessage.setRequestHeader('Content-Type', request.getHeader('Content-Type')); //Provide same header with boundary
restMessage.setRequestBodyFromAttachment(attachmentID);
restMessage.execute();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2024 10:38 AM
InternalError: Can't find method com.glide.ui.SysAttachment.writeContentStream(string,string,string,com.glide.communications.GlideScriptableInputStream). (sys_ws_operation.7f0aaf1a1bc1ce101c9054ee034bcb1c.operation_script; line 46)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 12:08 AM