Reputation: 189
I'm trying to write a Mule flow to poll a CRUD-web service. I've managed to do kind of what I'd like to do with a small JAVA-application but I'd like to use a HTTP-endpoint in a mule flow instead if its possible.
I'd like to do a HTTP-GET-request with Database ID in the HTTP-HEADER. Is this possible with a MULE-inbound-endpoint?
// HTTP GET request
private void sendGet() throws Exception {
String url = "https://XXXXXXXXXXXXXXXXXXXXXXXXXX";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("X-Appery-Database-Id", "XXXXXXXXXXXXXXXXXX");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
Upvotes: 0
Views: 1113
Reputation: 573
You can use MuleClient to execute a set of tests (CRUD operations), for example:
MuleClient client = muleContext.getClient();
Map parameters = new HashMap();
parameters.put("Content-Type", "application/json");
parameters.put("http.method", "GET"); // or POST, DELETE, PUT
parameters.put("DatabaseID", "ID");
then send to http-inbound-endpoint
MuleMessage response = client.send("http://localhost:8091/rest/domain", PAYLOAD, parameters);
assertNotNull(response);
assertThat(Integer.valueOf(response.getInboundProperty(HTTP_STATUS).toString()), is(200));
Victor shows you how to do it from a flow. I hope to help.
Upvotes: 1
Reputation: 5115
It is possible, the following:
<poll>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" method="GET">
<set-property propertyName="X-Appery-Database-Id" value="XXXXXXXXXXXXXXXXXX" />
<set-property propertyName="User-Agent" value="MY_USER_AGENT" />
</http:outbound-endpoint>
</poll>
will generate a request like this:
GET / HTTP/1.1
X-MULE_ENDPOINT: http://localhost:8081
X-Appery-Database-Id: XXXXXXXXXXXXXXXXXX
X-MULE_ROOT_MESSAGE_ID: 8c80b9f0-7682-11e4-94ea-12a3ae03ce3a
User-Agent: MY_USER_AGENT
X-MULE_SESSION: rO0ABXNyACNvcmcubXVsZS5zZXNzaW9uLkRlZmF1bHRNdWxlU2Vzc2lvbi7rdtEW7GGKAwAFWgAFdmFsaWRMAA1mbG93Q29uc3RydWN0dAAmTG9yZy9tdWxlL2FwaS9jb25zdHJ1Y3QvRmxvd0NvbnN0cnVjdDtMAAJpZHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wACnByb3BlcnRpZXN0AA9MamF2YS91dGlsL01hcDtMAA9zZWN1cml0eUNvbnRleHR0ACdMb3JnL211bGUvYXBpL3NlY3VyaXR5L1NlY3VyaXR5Q29udGV4dDt4cAFwdAAkOGM4MGI5ZjEtNzY4Mi0xMWU0LTk0ZWEtMTJhM2FlMDNjZTNhc3IAJWphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRNYXAbc/kJS0s5ewMAAkwAAW1xAH4AA0wABW11dGV4dAASTGphdmEvbGFuZy9PYmplY3Q7eHBzcgAkb3JnLm11bGUudXRpbC5DYXNlSW5zZW5zaXRpdmVIYXNoTWFwndHZ72dFzgADAAB4cHcMP0AAAAAAABAAAAAAeHEAfgAJeHB4
Host: localhost:8081
Upvotes: 1