Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set content type = "application/json" in mule.?

I have a HTML file which consists of a Form and it takes certain parameters from the User and when the URL is hit, a Mule service (written in Java) is called and it returns a JSON string. Now how can I set Content-type = application/json in either Mule or Java, so that browser know that it is getting a JSON?

My Mule config file is :

<custom-transformer name="HttpParams" class="org.mule.transport.http.transformers.HttpRequestBodyToParamMap" />
<message-properties-transformer name="HttpResponse">
    <add-message-property key="Content-Type" value="application/json" />
    <add-message-property key="http.status" value="303" />
</message-properties-transformer>

<flow name="jcars">
    <http:inbound-endpoint address="http://localhost:11221/jcars" transformer-refs="HttpParams" responseTransformer-refs="HttpResponse">
        <not-filter>
           <wildcard-filter pattern="/favicon.ico"/>
        </not-filter>
    </http:inbound-endpoint>

    <component class="defaults.basicapp.jcars"/>
</flow>

And my Java class is (Jcars) :

public String onEvent(Object obj){

     String json = "{{"id":0,"model":"suzuki","make":"2002","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2003","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2004","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2005","ps":true,"price":101.2,"bhp":12.6},{"id":0,"model":"suzuki","make":"2006","ps":true,"price":101.2,"bhp":12.6}}";

     return json;
}

The browser is displaying the string as it is. It does not know that the content type is application/json. What should I do?

like image 323
Raman Avatar asked Aug 04 '11 11:08

Raman


People also ask

What is set payload in Mule?

The Set Payload ( set-payload ) component lets you update the payload of the message. The payload can be a literal string or a DataWeave expression. The set-payload component, however, is not recommended for complex expressions or transformations but rather, simple ones, such as selections.

How do I change the default value in DataWeave?

Use one of the following methods to set default values when a payload value is absent or when the value is null : Using the default keyword. Setting the default in an if-else or else-if statement.

What is application types in Mule?

Mule Applications | MuleSoft Documentation. Replay CONNECT now. For IT TeamsAnypoint Platform World's #1 integration and API platform. Integration Studio Exchange Connectors DataGraph API management. Flex Gateway New.

What is artifact JSON in Mule 4?

Mule-artifact.json consists of: - Information about Your Apps. - configuration setting of App. - Required Mule version. - Class Loader Information.


1 Answers

Add a response message properties transformer in your flow in order to set the content type:

<response>
  <message-properties-transformer>
    <add-message-property key="Content-Type" value="application/json" />
  </message-properties-transformer>
</response>
like image 136
David Dossot Avatar answered Sep 27 '22 18:09

David Dossot