Attempting to POST attachment to 3rd party endpoint failure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2019 01:57 PM
Hello,
I am attempting to POST attachments from ServiceNow to OpsGenie.
I have gotten it to work via PostMan but when I try in ServiceNow, it results in the following error message:
Response status: 413, body: {"message":"The current request is not a multipart request","took":0.003,"requestId":"5e54093d-0fa4-4f60-8535-7857b2b7dd73"}
How my code works:
1) Business rule on sys_attachment table that encodes the file in base64 then sends that, along with the attachment sys_id to a script include
2) Script include receives that info and then creates the REST call, which it fires off to OpsGenie
Here is the relevant portion of the BR code:
// 2) Now that we've confirmed it's an OpsGenie inc, fire the REST call
gs.log('OpsGenie Create Attachment Alert: firing REST call from ZG - Send to OpsGenie BR on sys_attachment table','x_86994_opsgenie');
var StringUtil = new GlideStringUtil();
var sa = new GlideSysAttachment();
var binData = sa.getBytes(current);
var encData = StringUtil.base64Encode(binData);
var client = new x_86994_opsgenie.OpsGenie_Client();
client.postAttachmentToOpsGenieAlertAPI(current.sys_id, gr.number,encData,current.content_type);
gs.log("5 step","x_86994_opsgenie");
Script include code:
postAttachmentToOpsGenieAlertAPI: function(attach_sys_id, incNumber,encData,content_type){
try {
gs.info("1 Entering postAttachmentToOpsGenieAlertAPI","OpsGenie_Client");
// build the body, just needs to be the incNumber
var bodyContent = {
"alertIdentifier": "INC1102325",
"file":encData
};
var bodyContentJSON = new global.JSON().encode(bodyContent);
// build out our subEndPoint to fit into: ${endPoint}/v2/alerts${subEndPoint}
// target: https://api.opsgenie.com/v2/alerts/:alertIdentifier/attachments
// so build out: /:alertIdentifier/attachments
var subEndPoint = "/" + incNumber + "/attachments";
gs.info("Body:\n" + bodyContentJSON + "\nincNumber:\n" + incNumber + "\nencData:\n" + encData + "\ncontent_type:\n" + content_type,"OpsGenie_Client");
// create the REST message
var rest = new sn_ws.RESTMessageV2('x_86994_opsgenie.OpsGenie Alert API Endpoint', 'post');
rest.setRequestHeader("Content-Type", content_type);
rest.setRequestHeader("Authorization", "GenieKey " + this.apiKey);
rest.setQueryParameter("alertIdentifierType", "alias");
rest.setQueryParameter('file',encData);
rest.setStringParameter("endPoint", this.endpoint);
rest.setStringParameter("subEndPoint", subEndPoint);
rest.setRequestBody(bodyContentJSON);
// POST it
gs.info("Posting attachment " + attach_sys_id + " from " + incNumber + " to " + rest.getEndpoint(),'OpsGenie_Client');
var response = rest.execute();
// Parse response
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseLogMessage = "Response status: " + httpStatus + ", body: " + responseBody;
if(!httpStatus.toString().startsWith("2")){
gs.error(responseLogMessage,'OpsGenie_Client');
} else{
gs.info(responseLogMessage,'OpsGenie_Client');
}
}
catch(ex) {
var message = ex.getMessage();
gs.error("Exception occured: ", "OpsGenie_Client" + ex);
}
},
I'm not sure why it's returning a 413: current request is not a multi-part request. I verified that the data is being encoded properly and I am able to decode it. From what I found online, that error code occurs either when the content-type is not set correctly, or when the file is missing, but the file is being set in the query param (as the documentation states it should be) and in the body (something I was just testing out).
Thanks!
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 10:33 AM
Because I am in a scoped app I cannot call rest.getRequestBody() - however, I printed out the contents of body before I used rest.setRequestBody to set it. Here is the body:
-----WebKitFormBoundaryAAA
Content-Type:application/json
Content-Disposition:form-data
{"alertIdentifier":"INC1102325","file":"LSBmcmVlIGZvcm0gY29tbWVudHMgZmllbGQgdG8gTmV3IEhpcmUgVHJhdmVsIEZvcm0NCi0gcmVtb3ZlIFBlb3BsZSBPcHMgUGVuZGluZyBUaWNrZXRzIGZvcm0gZnJvbSBkYXNoYm9hcmQNCi0gYWRkIHRpY2tldHMgY2xvc2VkIHllYXIgdG8gZGF0ZSB0byBkYXNoDQotIFBlb3BsZSBPcHMgVGVhbSBpbmNpZGVudHMgYXNzaWduZWQgdG8gZWFjaCBwZXJzb24gc2hvdWxkIGJlIHJlbW92ZWQgZnJvbSBkYXNoDQotIGFkZCBwaWUgY2hhcnQganVzdCBmb3IgV29ya2RheSBzdWJjYXRlZ29yaWUNCi0gV2hlbiBwZW9wbGUgc2VuZCBhbiBlbWFpbCB0byBQZW9wbGUgT3BzIGRpc3RybywgdGhlIGNhbGxlcnMgZ2V0IHRoZSBlbWFpbCwgYnV0IHRoZSBQZW9wbGUgT3BzIHRlYW0gdGhlbXNlbHZlcyBkb24ndC4gVGlja2V0IGlzIGJlaW5nIGNyZWF0ZWQgc3VjY2Vzc2Z1bGx5LiBObyBvbmUgaW4gdGVhbSByZWNlaXZlcy4g"}
-----WebKitFormBoundaryAAA
Content-Type:text/plain
Content-Disposition:file;filename=peopleOpsAsks.txt;documentid=1
Content-Transfer-Encoding:base64
LSBmcmVlIGZvcm0gY29tbWVudHMgZmllbGQgdG8gTmV3IEhpcmUgVHJhdmVsIEZvcm0NCi0gcmVtb3ZlIFBlb3BsZSBPcHMgUGVuZGluZyBUaWNrZXRzIGZvcm0gZnJvbSBkYXNoYm9hcmQNCi0gYWRkIHRpY2tldHMgY2xvc2VkIHllYXIgdG8gZGF0ZSB0byBkYXNoDQotIFBlb3BsZSBPcHMgVGVhbSBpbmNpZGVudHMgYXNzaWduZWQgdG8gZWFjaCBwZXJzb24gc2hvdWxkIGJlIHJlbW92ZWQgZnJvbSBkYXNoDQotIGFkZCBwaWUgY2hhcnQganVzdCBmb3IgV29ya2RheSBzdWJjYXRlZ29yaWUNCi0gV2hlbiBwZW9wbGUgc2VuZCBhbiBlbWFpbCB0byBQZW9wbGUgT3BzIGRpc3RybywgdGhlIGNhbGxlcnMgZ2V0IHRoZSBlbWFpbCwgYnV0IHRoZSBQZW9wbGUgT3BzIHRlYW0gdGhlbXNlbHZlcyBkb24ndC4gVGlja2V0IGlzIGJlaW5nIGNyZWF0ZWQgc3VjY2Vzc2Z1bGx5LiBObyBvbmUgaW4gdGVhbSByZWNlaXZlcy4g
-----WebKitFormBoundaryAAA--
I also tried it with spaces before (like, Content-Type: application/json) and that didn't work either.
I also tried it without the "file" field in the JSON and same response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 10:43 AM
Hi Surya,
Can you try to send body as below: new line after content-disposition line;
also include your base64Encoded data again in place of encData and try once
-----WebKitFormBoundaryAAA
Content-Type: application/json
Content-Disposition: form-data
{
"alertIdentifier": "INC1102325"
}
-----WebKitFormBoundaryAAA
Content-Type:text/plain
Content-Disposition:file;filename=peopleOpsAsks.txt;documentid=1
Content-Transfer-Encoding:base64
encData
-----WebKitFormBoundaryAAA
nothing in the documentation is given for attachment; may be you can check with the team once if possible
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 11:01 AM
I tried that again and it didn't work:
-----WebKitFormBoundaryAAA
Content-Type:application/json
Content-Disposition:form-data
{"alertIdentifier":"INC1102325","file":"LSBmcmVlIGZvcm0gY29tbWVudHMgZmllbGQgdG8gTmV3IEhpcmUgVHJhdmVsIEZvcm0NCi0gcmVtb3ZlIFBlb3BsZSBPcHMgUGVuZGluZyBUaWNrZXRzIGZvcm0gZnJvbSBkYXNoYm9hcmQNCi0gYWRkIHRpY2tldHMgY2xvc2VkIHllYXIgdG8gZGF0ZSB0byBkYXNoDQotIFBlb3BsZSBPcHMgVGVhbSBpbmNpZGVudHMgYXNzaWduZWQgdG8gZWFjaCBwZXJzb24gc2hvdWxkIGJlIHJlbW92ZWQgZnJvbSBkYXNoDQotIGFkZCBwaWUgY2hhcnQganVzdCBmb3IgV29ya2RheSBzdWJjYXRlZ29yaWUNCi0gV2hlbiBwZW9wbGUgc2VuZCBhbiBlbWFpbCB0byBQZW9wbGUgT3BzIGRpc3RybywgdGhlIGNhbGxlcnMgZ2V0IHRoZSBlbWFpbCwgYnV0IHRoZSBQZW9wbGUgT3BzIHRlYW0gdGhlbXNlbHZlcyBkb24ndC4gVGlja2V0IGlzIGJlaW5nIGNyZWF0ZWQgc3VjY2Vzc2Z1bGx5LiBObyBvbmUgaW4gdGVhbSByZWNlaXZlcy4g"}
-----WebKitFormBoundaryAAA
Content-Type:text/plain
Content-Disposition:file;filename=peopleOpsAsks.txt;documentid=1
Content-Transfer-Encoding:base64
LSBmcmVlIGZvcm0gY29tbWVudHMgZmllbGQgdG8gTmV3IEhpcmUgVHJhdmVsIEZvcm0NCi0gcmVtb3ZlIFBlb3BsZSBPcHMgUGVuZGluZyBUaWNrZXRzIGZvcm0gZnJvbSBkYXNoYm9hcmQNCi0gYWRkIHRpY2tldHMgY2xvc2VkIHllYXIgdG8gZGF0ZSB0byBkYXNoDQotIFBlb3BsZSBPcHMgVGVhbSBpbmNpZGVudHMgYXNzaWduZWQgdG8gZWFjaCBwZXJzb24gc2hvdWxkIGJlIHJlbW92ZWQgZnJvbSBkYXNoDQotIGFkZCBwaWUgY2hhcnQganVzdCBmb3IgV29ya2RheSBzdWJjYXRlZ29yaWUNCi0gV2hlbiBwZW9wbGUgc2VuZCBhbiBlbWFpbCB0byBQZW9wbGUgT3BzIGRpc3RybywgdGhlIGNhbGxlcnMgZ2V0IHRoZSBlbWFpbCwgYnV0IHRoZSBQZW9wbGUgT3BzIHRlYW0gdGhlbXNlbHZlcyBkb24ndC4gVGlja2V0IGlzIGJlaW5nIGNyZWF0ZWQgc3VjY2Vzc2Z1bGx5LiBObyBvbmUgaW4gdGVhbSByZWNlaXZlcy4g
-----WebKitFormBoundaryAAA
I also attached the documentation for the createAlertAttachment in OpsGenie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 08:19 PM
Hi Surya,
But the sample request doesn't mention anything on the post method body
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2020 01:27 PM
You can send binary attachments from ServiceNow to Jira and other platforms with multipart, however it is pretty complicated.