How to validate content-type in mule4 - mule-studio

Hi I am having use case that I want to validate the content-type from request (Validate incoming request ie it has to be xml only if it is other than xml it has to print please check the content). How can we achieve this in mule4

You can get the content-type header from the attributes of the request.
Example
<flow name="testContentType">
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/"/>
<logger message="Content-type: #[attributes.Headers['content-type']]"/>
</flow>
You can use the same expression in a choice router or use the value to compare to a specific MIME type in a validator.

Related

Akka-HTTP: how to know if Content-type header was explicitly set in received response

Is the a way in akka-http to know if 'Content-type' header was explicitly set in HttpResponse that we received?
From sniffed Http dump I see, that there was no 'Content-Type' header, but
httpResponse.header[`Content-Type`].get.contentType.mediaType.toString()
and
httpResponse.entity.getContentType().mediaType.toString
stil return application/octet-stream.
This is default Content type not only for Akka-HTTP, but perhaps for other frameworks like Play too. Akka-http and other HTTP based technologies need to know how to parse content internally, based on this header. application/octet-stream means that it considers request body as just byte-stream.
Rule of thumb: if it is possible - try to specify Content-type.

REST request in SOAP UI

I am trying to make a REST POST request in SoapUI.. The XML contract for the same is
<Login>
<Email>admin</Email>
<Password>123456</Password>
</Login>
I have passed the values as follows in the REST request in SoapUI to the URL http://localhost:8080/SalesPropeller/rest/api/restLogin
Name Value Style level
Email admin Query Resource
Password 123456 Query Resource
How do I pass the parameters in SoapUI?
Should the root tag name be included?If yes, How?
It is based on how your backend expects the payload.
From XML Contract, Your request payload should be like this
<Login>
<Email>admin</Email>
<Password>123456</Password>
</Login>
If you want to add properties, to send variables as field values
<Login>
<Email>${propertyName}</Email>
<Password>${propertyName}</Password>
</Login>
For creating properties, refer http://www.soapui.org/functional-testing/properties/working-with-properties.html

How to send REST call in WSO2ESB with "Date" transport header

I'm trying to call a REST service from within WSO2ESB. The request needs to contain an authentication code, which will get computed based on some values including the Date transport header, which must also be part of the transport headers.
<syn:property name="Date" value="Mi, 1 Mrz 2015 11:00:00 MEZ" scope="transport" />
<syn:property name="X-Auth-Code" value="SomeCodeBasedOnDateHeader" scope="transport" />
<syn:send>
<syn:endpoint>
<syn:http uri-template="http://localhost:8280/rest/resourceA/{uri.var.resA}/resourceB/{uri.var.resB}" method="POST" />
</syn:endpoint>
</syn:send>
But when trying to send the request, the Date transport header will get removed by WSO2 ESB (The REST service will not get any Date header). Is there any chance to include the Date Header?
You can preserve the date by adding the following property to
D:\stack\wso2am-2.1.0\repository\conf\nhttp.properties
D:\stack\wso2am-2.1.0\repository\conf\passthru-http.properties
http.headers.preserve=Date
It seems that thoses headers are removed in both, NIO and Passthrough http transports :
Connection
Transfer-Encoding
Date
Content-Length
Keep-Alive
Server
User-Agent
You can preserve Server and User-Agent headers, setting http.server.preserve=true or http.user.agent.preserve=true in ESB_HOME/repository/conf/nhttp.properties or passthru-http.properties depending on which transport is configured in axis2, but it looks like there is no property to preserve Date header : you may be required to write your own http transport sender and configure your custom class in ESB_HOME/repository/conf/axis2/axis2.xml : <transportSender name="http" class="sss">

Mule REST APIKit- My flow isn't returning a messge body

**Issue solved, thanks to neildo. Here's what it took to get a response from a POST
RAML file must have a response body definition
HTTP Endpoint must be set to request-response
All Flows using the synchronous Processing Strategy
Accept and Content-Type HTTP headers set to application/json on the request
**
The implementation of my REST Flow calls a Java API that returns a Java object. I then convert the object to a JSON document and return it to the client. My Logger step is logging the correct data, however the client just gets a 200 OK with an empty body.
I have set all my Flows to synchronous and my HTTP Endpoint to request-response. What am I missing?
Thank you!
Nathan
Here is my XML file
<apikit:config name="IAMPerson-config" raml="IAMPerson.raml" consoleEnabled="true" consolePath="console" doc:name="Router"/>
<apikit:mapping-exception-strategy name="IAMPerson-apiKitGlobalExceptionMapping">
<apikit:mapping statusCode="404">
<apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
<set-property propertyName="Content-Type" value="application/json" />
<set-payload value="{ "message": "Resource not found" }" />
</apikit:mapping>
...
</apikit:mapping-exception-strategy>
<flow name="IAMPerson-main" doc:name="IAMPerson-main" processingStrategy="synchronous">
<http:inbound-endpoint address="http://localhost:8081/api" doc:name="HTTP" exchange-pattern="request-response" password="admin" user="admin" contentType="application/json"/>
<apikit:router config-ref="IAMPerson-config" doc:name="APIkit Router"/>
<exception-strategy ref="IAMPerson-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
</flow>
<flow name="post:/person:IAMPerson-config" doc:name="post:/person:IAMPerson-config" processingStrategy="synchronous">
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="PersonDTO"/>
<invoke name="invokeCreate" object-ref="personService" method="create" methodArguments="#[payload]"></invoke>
<json:object-to-json-transformer sourceClass="Person" doc:name="Person Object to JSON"/>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
<flow name="put:/person:IAMPerson-config" doc:name="put:/person:IAMPerson-config" processingStrategy="synchronous">
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="PersonDTO"/>
<invoke name="invokeUpdate" object-ref="personService" method="update" methodArguments="#[payload]"/>
<json:object-to-json-transformer sourceClass="Person" doc:name="Person Object to JSON"/>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
Here is part of my RAML file where I define a request and response body. When I posted a message to Mule, I got this error with nothing logged to the Mule console.
null (java.lang.NullPointerException). Message payload is of type: ContentLengthInputStream
post:
body:
application/json:
responses:
200:
body:
application/json:
In your raml file, make sure your resource method is mapping the response 200 body with application/json. For example...
/person:
post:
responses:
200:
body:
application/json:
I had the same problem (using version 3.5.0), but with a GET request. None of the proposed solutions (neither items 1 through 4 at the top of the edited question, nor the checked answer) solved the problem for me. (Most of them were already true in my case.)
What did work for me was adding the following at the bottom of the problematic GET flow's XML:
<response>
<set-property propertyName="Content-Type" value="application/json"/>
</response>
Unfortunately, there seems to be no way to do this using the visual editor (in the "Message Flow" tab). I had to do this manually in the "Configuration XML" tab instead.
Also, inserting the above element in the main flow (the one that invokes the APIKit Router) -- which has to be done BEFORE the <exception-strategy> element for some reason -- does not solve the problem. However, in this case, the response element DOES show up in the visual editor.
I looked at this page on SO 15 times before I realized how I'd caused this. I'd added a Transformer to do some authentication. I'd customized my onCall method, but left transformMessage as the default which sets a 200 and returns null.
Lo and behold, changing return null to return message made everything work again.
Use this format of response for 200 status code in your RAML file.
responses:
200:
body:
application/json:

Mule Rest Post call to SFDC to create a lead

I need to post a Rest call to SFDC with the respective credentials and create the SalesLead in SFDC. The response should come inthe form of Acknowledgment JSON response.
I am not sure how to use http:rest-service-component to post the data for creating lead.
Any help or sample is appreciated...
It is resolved now by using the "https:outbound-endpoint". All this code need is a JSON object as input.
Below is the snippnet:
<https:outbound-endpoint
method="POST" exchange-pattern="request-response"
address="url"
contentType="application/json" doc:name="HTTP" >
<message-properties-transformer scope="outbound">
<add-message-property key="Authorization" value="OAuth ****"/>
</message-properties-transformer>
</https:outbound-endpoint>
<echo-component doc:name="Echo"/>