Can I forward inbound api data stream to another endpoint?

Tae Kyung Lee
Tera Expert

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.

datastream.png

 

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--

 

 

4 REPLIES 4

GK15
Tera Contributor

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

Grzegorz Kaznoc
Tera Contributor

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();

 

 

 

    var attachmentID = new GlideSysAttachment().writeContentStream(
                    'incident', //grAttachmentHost,
                    'log.txt',
                    'text/plain',
                    request.body.dataStream);
 
Throws the following error. Any idea ?
error:
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)

bbgaurav
Tera Contributor

@Tae Kyung Lee  

@Ibrahim Abduwah 

were you able to get this working?