REST Multipart/form-data binary image request body?

Nick65
Mega Expert

So I have been working on an integration and was curious if anyone has successfully created a multipart/form-data outbound REST request with binary image data in Servicenow? With or without use of mid server. I know the request works because it works fine in Fiddler and Restify. Below is the body I am using, I have been able to post this exact message with a text file so I know the issue is with the binary I am sending. I have also tried base64. Does anyone have any tips or tricks for sending binary image data with Javascript? Any pitfalls or issues you have ran into in the past? Thanks !

--gc0p4Jq0M2Yt08jU534c0p

Content-Disposition: form-data; name="body"


goes with message

--gc0p4Jq0M2Yt08jU534c0p

Content-Disposition: form-data; name="attachment1"; filename="testpic.gif"

Content-Type:image/gif

Content-Transfer-Encoding: binary


${attachment}

--gc0p4Jq0M2Yt08jU534c0p--

1 ACCEPTED SOLUTION

Yes, I finally got this done with a custom .jar file and Javascript probe. I wrote a reply on


JavascriptProbe and MID Server Script Includes-John Andersen


I believe you must be the same Jonathan, let me know if you have any questions.


View solution in original post

15 REPLIES 15

Hi Nick,



I don't see your reply on JavascriptProbe and MID Server Script Includes-John Andersen I assume it is still pending moderation, and yes that comment was me! Do you have the scripts you used, would you mind posting them here?



Thanks!


Check the comment on the site again I left. I did this all in java and created a jar file and uploaded it to the mid server. So no actually scripts in servicenow to post, just calling the Javascript Probe.


Are you comfortable with Java?



What are you trying to POST? A file or text?


Thanks Nick, I took a look at that. Any way you can post the .jar file and the Javascript probe you used, it would be helpful to see how you went about it.



I am posting files.


Here is the probe script,



var jspr = new JavascriptProbe(this.mid);


jspr.addParameter("message", stripped_string);


jspr.addParameter("filename", file);


jspr.addParameter("type", type);


jspr.addParameter("encodedData", encData);


jspr.setName("Attach File to Post");


jspr.setJavascript("var res = new Packages.multipart.builder.rest.RestClient(); res.postMessage(probe.getParameter('encodedData'),probe.getParameter('filename'),probe.getParameter('message'),probe.getParameter('type'));");


var jdbc = jspr.create();



Here is the web site I used as examples.


Java Code Example for org.apache.commons.httpclient.methods.multipart.Part



Just make sure you are using the apache.commons.client, because I first tried to use the new apache http client   and org.apache.httpcomponents:httpmime and uploaded all the new libaries to the mid server, but had no luck with that so I stuck to the jars that come OOB with the Mid server.   The only jar I uploaded was the jar I exported after creating the below Class in Eclipse. This is the Java method I used



package.multipart.builder.rest;


public class RestClient{


public static void


  PostMethod post=new PostMethod("http://localhost:" + PORT + "/bookstore/books/image");
 
String ct="multipart/form-data";
  post
.setRequestHeader("Content-Type",ct);
 
Part[] parts=new Part[1];
  parts
[0]=new FilePart("image",new ByteArrayPartSource("testfile.png",new byte[1024 * 11]),"image/png",null);
  post
.setRequestEntity(new MultipartRequestEntity(parts,post.getParams()));
 
HttpClient httpclient=new HttpClient();
 
try {
   
int result=httpclient.executeMethod(post);
  assertEquals
(413,result);
 
}
 
finally {
  post
.releaseConnection();
 
}
}


}


Hi Nick,

 

I have the exact same requirement..is there anything you can share? Does this absolutely have to be done via a Javascript probe?