Basic flow results in NullPayload - mule

I'm new to Mule and I'm trying to get the very first example from the book "Mule in Action" working.
I'm using Mule 3.9 and Anypoint Studio 6.4.1. In chapter 1 they describe a very basic product_registration flow that I have created as follows:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jms
http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8880" basePath="products" doc:name="HTTP Listener Configuration"/>
<jms:activemq-connector name="Active_MQ" username="admin" password="admin" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="product_registrationFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger Before"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger level="INFO" doc:name="Logger After"/>
<jms:outbound-endpoint doc:name="JMS" queue="products"/>
</flow>
</mule>
and an accompanying functional test:
#Test
public void testCanRegisterProducts() throws Exception {
LocalMuleClient client = muleContext.getClient();
String productAsJson = "{ \"name\":\"Widget\", \"price\": 9.99, \"weight\": 1.0, \"sku\": \"abcd-56789\" }";
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
client.dispatch("http://localhost:8880/products", source);
MuleMessage result = client.request("jms://products", RECEIVE_TIMEOUT);
assertNotNull(result);
assertFalse(result.getPayload() instanceof NullPayload);
assertEquals(productAsJson, result.getPayloadAsString());
}
When I run the test it fails at the last assert because the actual payload is:
{NullPayload}
And if I look directly in ActiveMQ I see that payload. If I manually post to Mule (using a tool like Poster in Chrome, setting only the header Content-Type: application/json) the payload is valid JSON and I can get the test to pass (because it is getting the pending message from the queue posted by Poster and the message it creates is at the end of the queue with payload {NullPayload}.
Can someone shed some light on why the flow fails when invoked from the JUnit test, but seems to work when invoked by using a tool like Poster?
Update: With the help of Pierre B. I got it working. The initialization of the MuleMessage in the FunctionalTestCase was updated as follows:
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
source.setProperty("Content-Type", "application/json", PropertyScope.INBOUND);
source.setProperty("Content-Length", Integer.valueOf(productAsJson.length()), PropertyScope.INBOUND);

You said it works when you POST a message using an external tool instead of MuleClient:
using a tool like Poster in Chrome, setting only the header Content-Type: application/json
Try adding the same header to your MuleMessage such as:
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
# define the headers
Map<String, Object> headers = new HashMap<String, Object>(1);
headers.put("Content-Type", "application/json");
headers.put("Content-Length", sourceLength);
# add the headers as function parameter
client.dispatch("http://localhost:8880/products", source, headers);
EDIT: as #sceaj pointed out, both Content-Type and Content-Length headers are required.

Related

How to read file in middle of the flow

I am trying to read two simultaneously file in middle of the flow and combine them into one payload. To reading the files in middle of the flow I am using mule requester component. While triggering the flow (localhost:8081/requester/requester)I am getting error :
"Exception(s) were found for route(s):
0: The endpoint "src\main\resources\input1\employees.xml" is malformed and cannot be parsed. If this is the name of a global endpoint, check the name is correct, that the endpoint exists, and that you are using the correct configuration (eg the "ref" attribute). Note that names on inbound and outbound endpoints cannot be used to send or receive messages; use a named global endpoint instead. (org.mule.api.endpoint.MalformedEndpointException).
1: The endpoint "src\main\resources\input2\employees2.xml" is malformed and cannot be parsed. If this is the name of a global endpoint, check the name is correct, that the endpoint exists, and that you are using the correct configuration (eg the "ref" attribute). Note that names on inbound and outbound endpoints cannot be used to send or receive messages; use a named global endpoint instead. (org.mule.api.endpoint.MalformedEndpointException)."
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulerequester="http://www.mulesoft.org/schema/mule/mulerequester" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/mulerequester http://www.mulesoft.org/schema/mule/mulerequester/current/mule-mulerequester.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<file:connector name="file-connector-config" autoDelete="false" streaming="true" validateConnections="true" doc:name="File" />
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="/requester" doc:name="HTTP Listener Configuration" />
<flow name="muleRequester">
<http:listener config-ref="HTTP_Listener_Configuration" path="/requester" doc:name="HTTP" />
<logger message="Invoking Mule Requester" level="INFO" doc:name="Logger" />
<scatter-gather doc:name="Scatter-Gather">
<mulerequester:request resource="src\main\resources\input1\employees.xml" returnClass="java.lang.String" doc:name="Retrieve File1"/>
<mulerequester:request resource="src\main\resources\input2\employees2.xml" returnClass="java.lang.String" doc:name="Retrieve File2"/>
</scatter-gather><dw:transform-message doc:name="Transform Message">
<dw:set-payload>< [CDATA[%dw 1.0 %output application/json
---
{
payload1: payload[0],
payload2: payload[1]
}]]></dw:set-payload>
</dw:transform-message>
<file:outbound-endpoint path="src/main/resources/output" responseTimeout="10000" doc:name="File"/>
<logger message="Payload after file requester #[payload]" level="INFO" doc:name="Logger" />
</flow>
I am not using maven. Do I need to download any other jar or where I can do the correction?
resource needs to be an Mule endpoint url. Mule requester module can work with many transports such as jms, file, ftp. So the path to the file is not enough. Here is an example of an endpoint url for reading a file:
<mulerequester:request resource="file://src/main/resources/in/ReadME.txt?connector=file-connector-config" doc:name="Retrieve File" returnClass="java.lang.String" />
You can also point to a global endpoint like you have defined like so:
<mulerequester:request config-ref="muleRequesterConfig" resource="myFileEndpoint" doc:name="Mule Requester" />

MuleESB - Change Global Elements values at runtime for outbound connector

I tried to use the Webservice consumer connector as outbound Endpoint to consume the soap services and tried to set username and passwords dynamically. but I can't set values in this way. It is not parsing the MEL expression. How can I do ? Thank you
How are you setting the outboundProperties upstream in the flow? Are you sure you are not trying to use inboundProperties passed in from the Workday connector? This example works for me:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ws="http://www.mulesoft.org/schema/mule/ws" xmlns:cluster="http://www.mulesoft.org/schema/mule/ee/cluster"
xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd">
<ws:consumer-config name="Web_Service_Consumer" service="TicketServiceService" port="TicketServicePort" serviceAddress="http://training-u.cloudhub.io/essentials/delta" wsdlLocation="http://mu.mulesoft-training.com/essentials/delta?wsdl" doc:name="Web Service Consumer"/>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="Copy_of_mydomain1-app-oneFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/soap" doc:name="Copy_of_HTTP"/>
<set-property propertyName="workday_userid" value="WORKDAY_ID" doc:name="Copy_of_Property"/>
<set-property propertyName="workday_password" value="WORKDAY-PASSWORD" doc:name="Copy_of_Property"/>
<ws:consumer config-ref="Web_Service_Consumer" operation="listAllFlights" doc:name="Copy_of_Web Service Consumer"/>
<logger level="INFO" doc:name="Copy_of_Logger"/>
</flow>
</mule>
Add Dataweave before to webservice consumer and put below code in dataweave. It's working.
%dw 1.0
%output application/xml
%namespace wsse http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
%namespace wsu http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd
---
wsse#Security: {
wsse#UsernameToken #(Id: 'UsernameToken-18BC1F80151A19D55F14976049846641'): {
wsse#Username: 'john',
wsse#Password #(Type: 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'): 'doe',
wsse#Nonce #(EncodingType: 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'): 'EQDDem3KeXpbud4mJKpLVw==',
wsu#Created: '2017-06-16T09:23:04.662Z'
}
}
}
You can then add the the add property transformer before the webservice consumer (i had added the above as payload, so mapping from payload):
<set-property propertyName="soap.Security" value="#[message.payload]" doc:name="Property"/>
Also, remove the security settings from global web service consumer settings.

Handling Accept encoding in esb mule

I have mule flow look like below
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8111" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="api.bonanza.com" port="443" doc:name="HTTP Request Configuration" protocol="HTTPS"/>
<flow name="bonanza_fetchtoken_ceFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/add" allowedMethods="GET,POST" doc:name="HTTP" />
<flow-ref name="bonanza_addfixedpriceitemSub_Flow" doc:name="bonanza_addfixedpriceitemSub_Flow"/>
</flow>
<sub-flow name="bonanza_addfixedpriceitemSub_Flow">
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="X-BONANZLE-API-DEV-NAME" value="t******I"/>
<add-message-property key="X-BONANZLE-API-CERT-NAME" value="l*****F"/>
</message-properties-transformer>
<set-property propertyName="Content-Type" value="application/x-www-form-urlencoded" doc:name="Property"/>
<set-property propertyName="Accept" value="application/json" doc:name="Property"/>
<set-variable variableName="sim" value="{requesterCredentials:{bonanzleAuthToken:nWn1a9l3NT}}" doc:name="Variable"/>
<scripting:transformer returnClass="java.util.Map" doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[import groovy.json.JsonBuilder
m = [addFixedPriceItemRequest:'{requesterCredentials:{bonanzleAuthToken:n*****T}}']
builder = new JsonBuilder()
builder(m)
]]></scripting:script>
</scripting:transformer>
<logger message="payload :#[payload]" level="INFO" doc:name="Logger"/>
<http:request config-ref="HTTP_Request_Configuration" path="/api_requests/secure_request" method="POST" followRedirects="true" parseResponse="false" doc:name="HTTP">
<http:success-status-code-validator values="0..599"/>
</http:request>
<logger message="resp :#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
</sub-flow>
</mule>
I am able to receiving successful response from API using postman tool. I try to simulate same thing using above ESB mule flow. But API throws me an error as like below. Hence i have used requestb.in to compare the requests going from esb mule and postman tool. i found differences in the HTTP headers alone.
RAW body from postman to requestb.in -
addFixedPriceItemRequest={requesterCredentials:{bonanzleAuthToken:nW*****NT}}
RAW Body from ESB mule to requestb.in
addFixedPriceItemRequest=%7BrequesterCredentials%3A%7BbonanzleAuthToken%3AnW****NT%7D%7D
It seems that i have to serialize the json content before sending as content type - application/x-www-form-urlencoded. I found this info in this mule doc
How can i serialize json payload in groovy and sending as map payload?
API Error Response
{
"ack": "Failure",
"version": "1.0beta",
"timestamp": "2016-03-15T07:18:11.000Z",
"errorMessage": {
"message": "Cannot determine what type of request you are making. Often this can be the result of data that has not been escaped before being passed to the API. If you are passing data with quotation marks or other special characters, you should translate it to JSON, then escape it, before sending it over the API."
}
}
Please let me know for any additional info. Thanks in advance!
The flow got worked by adding key value pair in query parms in the http component. The flowVariable requestpayload has json string. So my Mule http component looks like below.
<http:request config-ref="HTTP_Request_Configuration" path="/api_requests/secure_request" method="POST" followRedirects="true" parseResponse="false" doc:name="HTTP">
<http:request-builder>
<http:query-param paramName="addFixedPriceItemRequest" value="#[flowVars.requestpayload]"/>
</http:request-builder>
<http:success-status-code-validator values="0..599"/>
</http:request>

Mule multiple response

I need to send multiple response from a mule flow one to a http endpoint which would be JSON and another to an SMTP endpoint where the recipient receives a custom email. If I use a transformer to transform the json respone which is default both endpoints receive the same custom email format. So how can I send the default JSON response to the http endpoint and the Custom Transformer generated email to the smtp endpoint below is my flow and Custom Transformer
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd">
<data-mapper:config name="json2_to_json" transformationGraphPath="json2_to_json.grf" doc:name="json_to_json"/>
<flow name="jirarestFlow3" doc:name="jirarestFlow3">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="6767" doc:name="HTTP" contentType="application/json"/>
<logger message="This is from hari #[message.payload]" level="DEBUG" doc:name="Logger"/>
<data-mapper:transform config-ref="json2_to_json" doc:name="JSON To JSON"/>
<logger level="DEBUG" doc:name="Logger" message="This is from Data Mapper #[json:fields/priority/id]"/>
<set-variable variableName="myPayload" value="#[json:fields/reporter/emailAddress]" doc:name="tmpPayload"/>
<logger message="MyPayload is #[myPayload]" level="DEBUG" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="9999" path="rest/api/2/issue/" method="POST" user="" password="" contentType="application/json" doc:name="HTTP"/>
<response>
<!--set-variable variableName="responsePayload" value="#[message.payload]" doc:name="respPayload"/-->
<!--http:response-builder doc:name="HTTP Response Builder" contentType="text/plain" status="200"/-->
<!--expression-transformer doc:name="Expression" /-->
<custom-transformer class="org.hhmi.transformer.EmailBodyTransformer" doc:name="Java"/>
<smtps:outbound-endpoint host="smtp.gmail.com" port="465" user="pandalai" password="soapui67" to="#[myPayload]" from="pandalai#gmail.com" subject="Jira Ticket" responseTimeout="10000" mimeType="text/plain" doc:name="SMTP">
<email:string-to-email-transformer doc:name="Email to String"/>
</smtps:outbound-endpoint>
</response>
</flow>
</mule>
Custom Transformer
package org.xxx.transformer;
import org.apache.log4j.Logger;
import org.xxx.dto.JsonBean;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class EmailBodyTransformer extends AbstractMessageTransformer {
public static Logger logger = Logger.getLogger(EmailBodyTransformer.class);
#Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
// TODO Auto-generated method stub
StringBuffer html = new StringBuffer();
Map<String,String> map = new HashMap<String,String>();
ObjectMapper mapper = new ObjectMapper();
JsonBean jBean = new JsonBean();
try {
logger.debug("EmailBodyTransformer payload "
+ message.getPayloadAsString());
logger.debug("EmailBodyTransformer class " + message.getClass());
map = mapper.readValue(message.getPayloadAsString(),
new TypeReference<HashMap<String,String>>(){});
jBean.setId(map.get("id"));
jBean.setKey(map.get("key"));
jBean.setSelf(map.get("self"));
System.out.println("EmailBodyTransformer id from map: "+ jBean.getId());
System.out.println("EmailBodyTransformer map: "+ map);
/* html.append("");
html.append("");
html.append("");
html.append("Dear ").append(jBean.getId()).append(" ");
html.append("Thank you for your order. Your transaction reference is: <strong>");
html.append(jBean.getKey()).append("</strong>");
html.append("");*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return message;
//return html;
}
}
The <response></response> tags mean, that whatever you put there will be put into your http:inbound-endpoint response. Since smtp is asynchronous, it will be ignored in the response phase, and the block will return whatever it gets from your custom transformer.
The solution is to move the contents inside the <response></response> block into an <async></async> block, so your custom transformer will not interfere with the http response. Inside the <response></response> block you will need just an <object-to-string-transformer> to transform your http:outbound-endpoint into a string.
EDIT:
Move the <object-to-string-transformer> after the http:outbound-endpoint and change your transformer to take String, not input stream. Otherwise the input stream gets closed once either of the two readers reads it. You will not need the <response></response> after this.
Your requirement is exactly what mule-requester-module is based on:
http://java.dzone.com/articles/introducing-mule-requester
example project: https://github.com/mulesoft/mule-module-requester
This module allows you to asynchronously do two things.
Hope this helps

Mule ESB - How to create a HTTP request with POST method (sending parameters along)

Short: I want to post a couple of parameters (like user=admin, key=12345678) using the POST method to a PHP page (like localhost/post-debug.php). The script would read the $_POST values and do whatever.
My questions are:
1. How can I get the example below to work?
2. How can I create the Map Payload with POST parameters from a JSON encoded payload and send it to the PHP script?
Below is an isolated case I am trying to get running (the parameters are "read" from the HTTP endpoint). I am calling directly from the browser the following URL:
http://localhost:8081/httpPost?user=admin&key=12345678
The underlying XML:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
<flow name="httpPostTestFlow1" doc:name="httpPostTestFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="httpPost" doc:name="HTTP"/>
<http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>
<echo-component doc:name="Echo"/>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost/post-debug.php" port="80" contentType="application/x-www-form-urlencoded" doc:name="HTTP" />
</flow>
</mule>
I am using MuleStudio 1.3.2, Mule ESB v.3.3.
I've reviewed many similar questions but none got me on the right track.
Here is the solution for question 2 (answering question 1 won't help):
<flow name="httpPostTestFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8081" path="httpPost" />
<json:json-to-object-transformer
returnClass="java.util.Map" />
<http:outbound-endpoint exchange-pattern="request-response"
host="localhost" port="80" path="post-debug.php" method="POST"
contentType="application/x-www-form-urlencoded" />
<copy-properties propertyName="*" />
</flow>
I've used the following to check it works fine:
curl -H "Content-Type: application/json" -d '{"param1":"value1","param2":"value2"}' http://localhost:8081/httpPost
Note that I use copy-properties to propagate all the response headers from the PHP script invocation back to the original caller. Remove it if you don't care.
Have you tried configuring your outbound endpoint like this:
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="80" path="post-debug.php" contentType="application/x-www-form-urlencoded" doc:name="HTTP" method="POST"/>
Questions a little old, but just hit the same problem. I never could get the "body-to-parameter-map-transformer" to work, so I threw in a custom java component. It parses a URLEncoded param string into a HashMap. Set your vars based on that.
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
public class ParseParams {
public static HashMap<String, String> jsonToMap(String t) throws JSONException {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while( keys.hasNext() ){
String key = (String)keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
return map;
}
}