简体   繁体   中英

How to flush xml data in request by using HTTP connector in mule

I am using java component to send request and it is working fine for me. The code is.

 public class parseXmlToString implements Callable 
    {
        public Object onCall(MuleEventContext eventContext) throws Exception 
        {
            String xmlData = eventContext.getMessage().getInvocationProperty("sampleXmlData");
            String content = URLEncoder.encode(xmlData, "UTF-8");
            eventContext.getMessage().setInvocationProperty("xmlData", content);

            try
            {
                URL url = new URL(webServiceURL);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.connect();

                DataOutputStream output = null;
                output = new DataOutputStream(conn.getOutputStream());

                String content = "xmlData=" + URLEncoder.encode(xmlData, "UTF-8");

                output.writeBytes(content);
                output.flush();
                output.close();

                if (conn.getResponseCode() != 200) 
                {
                    System.out.println(conn.getResponseCode());
                    throw new IOException(conn.getResponseMessage());
                }

                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = rd.readLine()) != null) 
                {
                    sb.append(line);
                }

                rd.close();
                conn.disconnect();
                eventContext.getMessage().setPayload("Import Initiated");
            }
            catch (Exception e)
            {
                System.out.println("Failed REST service call. " + e.getMessage());
                e.printStackTrace();
            }
            eventContext.getMessage().setPayload(content);
            return eventContext.getMessage().getPayload();enter code here
        }

    }

Now i am trying to send request by using HTTP connector in mule and for that i am sending data with request parameter. The code for this is

<http:request config-ref="HTTP_Request_Configuration1" path="/#flowVars.outputAppName]/services/EmployeeService/importUsers" method="POST" doc:name="HTTP" parseResponse="false">
<http:request-builder> 
<http:query-param paramName="serviceId" value="#[flowVars.outputServiceID]"/>
<http:query-param paramName="expiresOn" value="#[flowVars.expiresOnImport]"/>
<http:query-param paramName="importId" value="#[flowVars.importId]"/>
<http:query-param paramName="signature" value="#[flowVars.signatureImport]"/>
<http:header headerName="xmlData" value="#[flowVars.xmlData]"/>
</http:request-builder>
 </http:request>

if i run it then i got following error

    ERROR 2015-03-09 11:03:31,251 [[catalystoneconnector].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
    ********************************************************************************
    Message               : Response code 500 mapped as failure. Message payload is of type: BufferInputStream
    Code                  : MULE_ERROR--2
    --------------------------------------------------------------------------------
    Exception stack is:
    1. Response code 500 mapped as failure. Message payload is of type: BufferInputStream (org.mule.module.http.internal.request.ResponseValidatorException)
      org.mule.module.http.internal.request.SuccessStatusCodeValidator:37 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/http/internal/request/ResponseValidatorException.html)
    --------------------------------------------------------------------------------
    Root Exception stack trace:
    org.mule.module.http.internal.request.ResponseValidatorException: Response code 500 mapped as failure. Message payload is of type: BufferInputStream
        at org.mule.module.http.internal.request.SuccessStatusCodeValidator.validate(SuccessStatusCodeValidator.java:37)
        at org.mule.module.http.internal.request.DefaultHttpRequester.innerProcess(DefaultHttpRequester.java:202)
        at org.mule.module.http.internal.request.DefaultHttpRequester.process(DefaultHttpRequester.java:166)
        + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
    ********************************************************************************

I've also tried sending the XML data as a query-param just like other parameters in the http connector. It doesn't work too.

What am I doing wrong or is there any proper way of doing this in the Mule way? Like sending a large xml in the request?

You need to add a entity body to your POST request. Here is an example from the documentation :

<set-payload value="Hello world" />
<http:request request-config="HTTP_Request_Configuration"
      path="test" method="GET" sendBodyMode="ALWAYS"  />

So for your case, you want:

<set-payload value="#[flowVars.xmlData]"/>

<http:request config-ref="HTTP_Request_Configuration1"
      path="/#flowVars.outputAppName]/services/EmployeeService/importUsers"
      method="POST" parseResponse="false"
      sendBodyMode="ALWAYS">
  <http:request-builder> 
    <http:query-param paramName="serviceId" value="#[flowVars.outputServiceID]"/>
    <http:query-param paramName="expiresOn" value="#[flowVars.expiresOnImport]"/>
    <http:query-param paramName="importId" value="#[flowVars.importId]"/>
    <http:query-param paramName="signature" value="#[flowVars.signatureImport]"/>
  </http:request-builder>
</http:request>

i would suggest to look at definition of http path provided as below /#flowVars.outputAppName]/services/EmployeeService/importUsers

Could you try by redefining as below
#['/'+flowVars.outputAppName+'/services/EmployeeService/importUsers']

and the other thing was as @David suggest set the xml data using set-payload component and define the http component with required set of query parameters and http connector method="POST"

Don't know if this helps but I was searching for a way to flush the xml data and got to this topic. May not answer exactly to this question but if someone comes here as I did looking for an answer to the same doubt I had is this:

One way to flush xml data from the response is this:

...with Axis2 + SOAP 1.2

OMElement omElement = payloadTypeResponse.getOMElement(ServiceEMEStub.QueryData.MY_QNAME,OMAbstractFactory.getSOAP12Factory());
System.out.println(omElement.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM