0
votes

I have a service endpoint http://xyzhost:port/greet123 and it accept request parameters salary and age and I need to pass query parameters as part of request like http://xyzhost:port/greet123?salary=100000&age=32 . I am new to mule , I am successful in invoking other service endpoints which do not have request parameters but when there are query parameters to be passed I am unsuccessful to set query parameters and to get proper response. I am using mule 3.3 version.Below is my mule flow. In mule flow before hitting service endpoint using http:outbound-endpoint I am using custom-transformer (custom request transformer) to set query parameters to outbound request scope. But no luck , unable to set query parameters as part of request due to that I got 400 - Bad request as response from service endpoint. Even I also tried using in mule flow. Can anyone please suggest me the way how to set query parameters to service endpoint.

Below is mule flow xml file and custom request transformer

<http:connector name="ClientOutboundHttpConnector">
       <service-overrides sessionHandler="org.mule.session.NullSessionHandler"/>
    </http:connector>
   
    
<!--  Below is the flow for sample rest endpoint -->

<custom-transformer
        name="SampleRequestTransformer"
        class="com.example.SampleRequestTrasformer" />
        
        <custom-transformer
        name="SampleResponseTransformer"
        class="com.example.SampleResponseTrasformer" />
    
    <flow name="sampleRESTFlow" doc:name="sample rest Flow">
                     
        <inbound-endpoint address="servlet://getDetails" exchange-pattern="request-response" connector-ref="muleCXFServletConnector" 
                transformer-refs="SampleRequestTransformer"  responseTimeout="600000" />
                
        
        
        <http:outbound-endpoint
            address="http://xyzhost:port/greet123"
            connector-ref="ClientOutboundHttpConnector"
            exchange-pattern="request-response"
            contentType="application/x-www-form-urlencoded"
            keep-alive="true"
            responseTimeout="360000"
            responseTransformer-refs="InputStreamToStringTransformer"/>

        <logger
            message="MappingServer Response after reading stream=#[payload]"
            level="INFO" />

        <transformer ref="SampleResponseTransformer" />

        <transformer ref="clientErrorResponseTransformer" />

        

    </flow>

Below is the code for custom request transformer


    @Override
    public Object transformMessage(MuleMessage message, String arg1)
            throws TransformerException {
        // TODO Auto-generated method stub
        Object payload = message.getPayload();
        Map<String,String> queryParams= new HashMap<String,String>();
        queryParams.put("salary", "400000");
        queryParams.put("age", "32");
        
        
        
    
        message.setProperty("Content-Type", "application/x-www-form-urlencoded", PropertyScope.OUTBOUND);
        message.setProperty("mimeType","application/x-www-form-urlencoded",PropertyScope.OUTBOUND);
        message.setProperty("http.method", "POST", PropertyScope.OUTBOUND);
        message.setProperty("http.query.params",queryParams, PropertyScope.OUTBOUND);
        
        
        return message;
    }

}```
    
2
Why are you using such an old version? It very old, unsupported a long time and no one uses it. Can you use a current version?aled

2 Answers

0
votes

First - http error 400 means that resources is not available. But server and port are fine. So, /greet123 is not available. Issue is not with your code - issue is nobody is listening for it. Try regular browser or Postman - result will be the same - 400 error.

Second - Mule 3 is deprecated. As I know 3.8 is going out of support this year. 3.3 is long time gone. Mule 4 is the current version and there is no way Mule 3 could be transferred to Mule 4.

https://simpleflatservice.com/mule4/Mule3toMule4Transformation.html

0
votes

You just add the query parameters to the end of the endpoint address after a question mark.

I would use MEL though I haven't tested it in 3.3. It may need a different syntax. You should put the values into a flowVar (invocation property) though.

Example:

    <http:outbound-endpoint
        address="#['http://xyzhost:port/greet123?salary=' + flowVar.queryParams.salary + ..."]

The HTTP Requester in Mule 3.9 has a much clearer syntax and you don't need any Java to do this. Probably you don't need Java in 3.3 either.