-
-
Save jakeceballos/24cd89351aa702a0ed174ca693af93ca to your computer and use it in GitHub Desktop.
var body = {} | |
var endpoint = 'https://localhost/'; | |
var method = 'POST'; | |
// Create connection | |
var url = new java.net.URL(endpoint); | |
var conn = url.openConnection(); | |
conn.setDoOutput(true); | |
conn.setDoInput(true); | |
conn.setRequestMethod(method); | |
conn.setRequestProperty("Content-Type", "application/json"); | |
// Send request | |
var outStream = conn.getOutputStream(); | |
var outWriter = new java.io.OutputStreamWriter(outStream); | |
outWriter.write(body); | |
outWriter.close(); | |
var inputStream = conn.getInputStream(); | |
var streamReader = new java.io.InputStreamReader(inputStream); | |
var respStream = new java.io.BufferedReader(streamReader); | |
var buffer = new java.lang.StringBuffer(); | |
var line = null; | |
while ((line = respStream.readLine()) != null) { | |
buffer.append(line); | |
} | |
respStream.close(); | |
var result = buffer.toString(); |
@jakeceballos have you been able to get this working for content ty[pes of application/x-www-form-urlencoded ?
@mochsner this snippet of code expects the body to be a json payload. To make it work for form-data you’ll need to build the data payload (eg field1=value1&field2=value2) and set that as the body instead of {}.
I haven’t tried posting a form but i assume it should work the same.
You also might want to look at using the Apache common httpclient. It’s included with mirth out of the box and is pretty easy to use.
@jakeceballos yeah, I think you're right. I've ran into an issue with handling on this previous method. It works great otherwise (for my OAuth token generation), but my actual server requests appear to be... well, hard to diagnose issues for.
Trying to use something like below from https://stackoverflow.com/questions/61649942
var result;
// Using block level Java class imports
with (JavaImporter(
org.apache.commons.io.IOUtils,
org.apache.http.client.methods.HttpPost,
org.apache.http.client.entity.UrlEncodedFormEntity,
org.apache.http.impl.client.HttpClients,
org.apache.http.message.BasicNameValuePair,
com.google.common.io.Closer))
{
var closer = Closer.create();
try {
var httpclient = closer.register(HttpClients.createDefault());
var httpPost = new HttpPost('http://localhost:9919/myphpscript.php');
// javascript array as java List
var postParameters = [
new BasicNameValuePair("firstname", "John"),
new BasicNameValuePair("lastname", "Smith")
];
// Rhino JavaBean access to set property
// Same as httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
httpPost.entity = new UrlEncodedFormEntity(postParameters, "UTF-8");
var response = closer.register(httpclient.execute(httpPost));
// Rhino JavaBean access to get properties
// Same as var is = response.getEntity().getContent();
var is = closer.register(response.entity.content);
result = IOUtils.toString(is, 'UTF-8');
} finally {
closer.close();
}
}
logger.info(result);
Thank you for this. This helped me a lot. Do you have an example of a http-request-post for a CSV file upload?