Receiving payload errors when trying to connect to REST API - api

I am looking to call an internal REST API and pass the payload back in Mule 3.7.3. The request is only the URL and no body but I am getting payload errors like nullPayload.
How can I get this to work?
I am testing it with this public API https://apisandbox.openbankproject.com/obp/v2.0.0/banks
and the code I have mocked up is here:
<?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:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.7.3"
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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request" host="https://apisandbox.openbankproject.com" port="443" basePath="/obp/v2.0.0" doc:name="HTTP Request Configuration"/>
<flow name="account-balance-flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="#['{}']" doc:name="Set Payload"/>
<http:request config-ref="HTTP_Request" path="/banks" method="GET" doc:name="HTTP"/>
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>

Host should not contain a URL. It should just be the host, and HTTPS protocol specified separately. Also no need to set the payload for a GET request. updated code that works:
<?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:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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.7.3"
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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request" protocol="HTTPS" host="apisandbox.openbankproject.com" port="443" basePath="/obp/v2.0.0" doc:name="HTTP Request Configuration"/>
<flow name="account-balance-flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP"/>
<http:request config-ref="HTTP_Request" path="/banks" method="GET" doc:name="HTTP"/>
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>

Related

Oauth 2.0 access token issue

I am consuming one of the Google API's and provided all the OAuth 2.0 credentials in HTTP requester.I need to generate the access token, capture the token and pass this access token in the headers to get the output.But I am getting the error:
Execution of the expression "'Bearer' + payload.access_token" failed.
(org.mule.api.expression.ExpressionRuntimeException).
Is this the correct way of generating the token and accessing it?
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2" 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/oauth2 http://www.mulesoft.org/schema/mule/oauth2/current/mule-oauth2.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="www.googleapis.com" port="443" doc:name="HTTP Request Configuration">
<oauth2:authorization-code-grant-type clientId="client_id" clientSecret="client_secret" redirectionUrl="http://localhost:8082/callback">
<oauth2:authorization-request authorizationUrl="https://accounts.google.com/o/oauth2/auth" localAuthorizationUrl="http://localhost:8082/login" scopes="https://www.googleapis.com/auth/admin.directory.user"/>
<oauth2:token-request tokenUrl="https://accounts.google.com/o/oauth2/token">
<oauth2:token-response accessToken="#[payload.access_token]"/>
</oauth2:token-request>
</oauth2:authorization-code-grant-type>
</http:request-config>
<flow name="csdfFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<http:request config-ref="HTTP_Request_Configuration" path="/admin/directory/v1/users?domain=aabbcc.com" method="GET" doc:name="HTTP">
<http:request-builder>
<http:header headerName="Authorization" value="#['Bearer '+payload.access_token]"/>
</http:request-builder>
<http:success-status-code-validator values="0..599"/>
</http:request>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
The expression seems to be wrong.
Try value="Bearer #[payload.access_token]"
you can pass Bearer token in header as shown below : Try like this:
try this it may help you:
<http:header headerName="Authorization" value="Bearer
#[flowVars.accesstoken]"/>

Mule esb and Transform message (wave transform component)

i have a problem working with transform message component and mule ESB that I do not understand.
I have a simple flow that works fine, at the beginning it is Http endpoint and at the end it is Transform message component, transforms one JSON to another.
Now when I try to store payload before transform message to variable and then set payload to that variable, I get exception on transform message component:
Type mismatch
found :name, :string
required :name, :object (com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchException)
com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchNode:65 (null)
Here is a flow that works, and below that is the flow that doesn't work.
Flow that works:
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<dw:transform-message metadata:id="b8a77df6-4692-4c52-b572-b6a175e7467e" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
id: payload.transfer-id,
created-record-status: 'accepter'
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
Flow that doesn't work>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<set-variable variableName="OriginalPayload" value="#[payload]" doc:name="Variable"/>
<set-payload value="#[flowVars.OriginalPayload]" doc:name="Set Payload"/>
<dw:transform-message metadata:id="b8a77df6-4692-4c52-b572-b6a175e7467e" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
id: payload.transfer-id,
created-record-status: 'accepter'
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
When i put logger to log payload before transformation, it is the same in both flows....
Can somebody tell me what am I doing wrong?
Thanks Ivan.
since the set payload is missing mime type you are getting the error use the below xml,I have tested and its working fine.<set-payload value="#[flowVars.OriginalPayload]" doc:name="Set Payload" mimeType="application/json"/>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Confi" host="localhost" port="8085" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Confi" path="/test" doc:name="HTTP"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<set-variable variableName="OriginalPayload" value="#[payload]" doc:name="Variable"/>
<set-payload value="#[flowVars.OriginalPayload]" doc:name="Set Payload" mimeType="application/json"/>
<dw:transform-message metadata:id="b8a77df6-4692-4c52-b572-b6a175e7467e" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
id: payload.transfer-id,
created-record-status: 'accepter'
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>

Read single row at time from mule and transform to SOAP request in mule

I am facing one issue.
Requirement is I have a csv file and should transform to soap request (soap consumer component). Problem is all csv rows are transformed in one shot. In my case soap request can not handle all the rows in one request. It should be multiple call.
What I did is,
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:ws="http://www.mulesoft.org/schema/mule/ws" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" 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" version="EE-3.5.0"
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/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
<ws:consumer-config name="Web_Service_Consumer1" wsdlLocation="CPAdministration_updated.wsdl" service="AdminService" port="AdminEndpoint" serviceAddress="http://localhost/FM/service/Hrportal?wsdlformule" doc:name="Web Service Consumer"/>
<data-mapper:config name="CSV_To_Xml_UpdateUserRequest_" transformationGraphPath="csv_to_xml_updateuserrequest__2.grf" doc:name="CSV_To_Xml_UpdateUserRequest_"/>
<flow name="soapFlow1" doc:name="soapFlow1">
<file:inbound-endpoint path="E:/temp/mule" responseTimeout="10000" doc:name="File"/>
<data-mapper:transform config-ref="CSV_To_Xml_UpdateUserRequest_" doc:name="CSV To Xml<UpdateUserRequest>" />
<logger message="Converted xml is here #[payload]" level="INFO" doc:name="Logger"/>
<ws:consumer config-ref="Web_Service_Consumer1" operation="UpdateUser" doc:name="Web Service Consumer"/>
</flow>
</mule>
One way to accomplish this is to use a splitter and an aggregator. Your flow might look something like this:
<flow name="soapFlow1" doc:name="soapFlow1">
<file:inbound-endpoint path="E:/temp/mule" responseTimeout="10000" doc:name="File"/>
<data-mapper:transform config-ref="CSV_To_Xml_UpdateUserRequest_" doc:name="CSV To Xml<UpdateUserRequest>" />
<logger message="Converted xml is here #[payload]" level="INFO" doc:name="Logger"/>
<splitter expression="#[xpath('//item')]" doc:name="Splitter" enableCorrelation="ALLWAYS"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<ws:consumer config-ref="Web_Service_Consumer1" operation="UpdateUser" doc:name="Web Service Consumer"/>
<message-chunk-aggregator failOnTimeout="true" doc:name="Message Chunk Aggregator"/>
</flow>
The xpath expression obviously has to be changed to something that makes sense in your case.

Google Task/Calendar

I am playing with Google Task connector and ended up with following error
The content of element 'google-tasks:config-with-oauth' is not complete. One of '{"http://www.mulesoft.org/schema/mule/core":annotations, "http://www.mulesoft.org/schema/mule/google-tasks":oauth-callback-config}' is expected.
Here is my configuration
<google-tasks:config-with-oauth name="Google_Tasks"
consumerKey="sagitec.mygbiz.com"
consumerSecret="oeX9wb_GhldQJYjHKLDqC-EB" doc:name="Google Tasks"/>
<flow name="google_taskFlow1" doc:name="google_taskFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<google-tasks:authorize config-ref="Google_Tasks" accessTokenUrl="https://accounts.google.com/o/oauth2/token"
authorizationUrl="https://accounts.google.com/o/oauth2/auth"
access_type="online" force_prompt="auto" doc:name="Google Tasks"/>
<logger level="INFO" doc:name="Logger" message="#[payload]"/> </flow>
If i enter "localhost" in Domain (which is specified as optional) under Oauth tab for google-tasks:config-with-oauth, i am not getting any build error.
So my first question is, what value i have to enter under Domain and where i can get it from.
2) What is the difference between Consumer Key/Secret and ClientKey/secret.
3) in one of the sample, i saw ${google.apiKey}. What is this and if this is a variable, wgere and how to declare it.
It will be really nice if any sample provided.
Thanks in advance,
Kannan
here i added sample example for google calender authentication using google connector
your
client id >>consumerKey
client secret >> consumerSecret
${google.apiKey} >> your global google calendar config name i.e. name="Google_Calendars" in below flow
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data- mapper" xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:objectstore="http://www.mulesoft.org/schema/mule/objectstore"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:google-calendars="http://www.mulesoft.org/schema/mule/google-calendars"
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.5.2"
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.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/google-calendars http://www.mulesoft.org/schema/mule/google-calendars/1.0/mule-google-calendars.xsd
http://www.mulesoft.org/schema/mule/objectstore http://www.mulesoft.org/schema/mule/objectstore/1.0/mule-objectstore.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
<google-calendars:config-with-oauth
name="Google_Calendars"
consumerKey="${consumer-key}"
consumerSecret="${consumer-secret}" doc:name="Google Calendars"
applicationName="test">
<google-calendars:oauth-callback-config
domain="localhost" localPort="8083" path="list"
remotePort="8083" />
<google-calendars:oauth-store-config objectStore-ref="ObjectStoreBean" />
</google-calendars:config-with-oauth>
<spring:beans>
<spring:bean id="ObjectStoreBean" name="ObjectStoreBean"
class="org.mule.util.store.SimpleMemoryObjectStore" />
</spring:beans>
<flow name="authorizationAndAuthenticationFlow" doc:name="authorizationAndAuthenticationFlow">
<http:inbound-endpoint host="localhost" port="8080"
path="oauth-authorize" doc:name="HTTP" />
<google-calendars:authorize config-ref="Google_Calendars"
doc:name="Google Calendars" />
<http:response-builder status="200"
doc:name="HTTP Response Builder">
<set-payload value="You have successfully authorized the connector" />
</http:response-builder>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<http:response-builder status="404"
doc:name="HTTP Response Builder">
<set-payload value="An error has occurred authorizing the connector" />
</http:response-builder>
</catch-exception-strategy>
</flow>
</mule>

Mule file download with HTTP Endpoint

My HTTP Endpoint responsible for downloading the file at the end of the flow is erroring out. It keeps trying to communicate with http://:80/ instead of the URL passed in. What am I doing wrong here?
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:https="http://www.mulesoft.org/schema/mule/https" 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.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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/https http://www.mulesoft.org/schema/mule/https/current/mule-https.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/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd ">
<flow name="BingFlow1" doc:name="BingFlow1">
<http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" doc:name="HTTP"/>
<https:outbound-endpoint exchange-pattern="request-response" host="api.datamarket.azure.com" port="443" path="Data.ashx/Bing/Search/v1/Web?Query=%27contract%20california%27&WebFileType=%27PDF%27&$top=50&$format=Json" user="*****" password="*****" doc:name="Bing"/>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<expression-transformer expression="#[message.payload.d.results]" doc:name="Expression"/>
<collection-splitter doc:name="Collection Splitter"/>
<expression-transformer expression="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://')]" doc:name="Expression"/>
<logger message="Payload is: #[message.payload]" level="INFO" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response" host="#[org.mule.util.StringUtils.substringBefore(message.payload, '/')]" port="80" method="GET" doc:name="HTTP" contentType="application/pdf" mimeType="application/pdf" path="#[org.mule.util.StringUtils.substringAfter(message.payload, '/')]"/>
<file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[org.mule.util.StringUtils.replace(message.payload, '/','.')]" path="/home/user/Documents/output" mimeType="application/pdf"/>
</flow>
</mule>
When changed to match the first answer, I get this exception:
********************************************************************************
Message : Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Message payload is of type: LinkedHas
hMap
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Message payload is of type: LinkedHashMap (org.mule.transp
ort.http.components.RestServiceException)
org.mule.transport.http.components.RestServiceWrapper:219 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/transport/http/components/RestServiceException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.transport.http.components.RestServiceException: Failed to invoke REST service "http://santaclaraca.gov/modules/ShowDocument.aspx?documentid=108?followRedirects=true". Mess
age payload is of type: LinkedHashMap
at org.mule.transport.http.components.RestServiceWrapper.doInvoke(RestServiceWrapper.java:219)
at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:126)
at org.mule.component.AbstractComponent.access$000(AbstractComponent.java:61)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
--- SOLVED ---
Big thanks to David. This is my final solution:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:https="http://www.mulesoft.org/schema/mule/https" 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.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.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/https http://www.mulesoft.org/schema/mule/https/current/mule-https.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 ">
<configuration doc:name="Configuration">
<expression-language>
<import class="org.mule.util.StringUtils" />
</expression-language>
</configuration>
<flow name="BingQuery" doc:name="BingQuery">
<http:inbound-endpoint exchange-pattern="one-way"
host="localhost" port="8082" doc:name="HTTP" />
<https:outbound-endpoint exchange-pattern="request-response"
host="api.datamarket.azure.com" port="443"
path="Data.ashx/Bing/Search/v1/Web?Query=%27california%20school%20district%20contract%27&WebFileType=%27PDF%27&$top=10&$format=Json"
user="*****" password="*****"
doc:name="Bing" />
<json:json-to-object-transformer
returnClass="java.util.Map" doc:name="JSON to Object" />
<expression-transformer expression="#[message.payload.d.results]"
doc:name="Expression" />
<collection-splitter doc:name="Collection Splitter" />
<vm:outbound-endpoint exchange-pattern="one-way"
doc:name="VM" path="fileWriter" />
</flow>
<flow name="RestProcessor" doc:name="RestProcessor">
<vm:inbound-endpoint exchange-pattern="one-way"
doc:name="VM" path="fileWriter" />
<set-variable variableName="fileName" value="#[message.payload.ID].pdf"
doc:name="Variable" />
<http:rest-service-component
serviceUrl="#[joinChar=message.payload.Url.contains('?')?'&':'?' ; StringUtils.join(new String[]{message.payload.Url,(String)joinChar,'followRedirects=true'})]"
httpMethod="GET">
<http:error-filter>
<expression-filter
expression="#[Integer.valueOf(message.inboundProperties['http.status']) >= 400]"></expression-filter>
</http:error-filter>
</http:rest-service-component>
<file:outbound-endpoint responseTimeout="10000"
doc:name="File" outputPattern="#[flowVars.fileName]" path="/home/ken/Documents/output"
mimeType="application/pdf" />
</flow>
</mule>
I can't spot the error: your flow looks good (except a problem I'll detail below) so, alternatively to using an http:outbound-endpoint, which can be finicky at times, you could use the http:rest-service-component.
Your next problem will be in the outputPattern of the file endpoint: remember at this point message.payload will be the response from the HTTP endpoint not the (partial) URL anymore. You need to pre-compute the file name and store it in a flow variable, then use it.
This would give:
<configuration>
<expression-language>
<import class="org.mule.util.StringUtils" />
</expression-language>
</configuration>
<flow name="BingFlow1" doc:name="BingFlow1">
<http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" doc:name="HTTP"/>
<https:outbound-endpoint exchange-pattern="request-response" host="api.datamarket.azure.com" port="443" path="Data.ashx/Bing/Search/v1/Web?Query=%27contract%20california%27&WebFileType=%27PDF%27&$top=50&$format=Json" user="*****" password="*****" doc:name="Bing"/>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<expression-transformer expression="#[message.payload.d.results]" doc:name="Expression"/>
<collection-splitter doc:name="Collection Splitter"/>
<set-variable variableName="fileName" value="#[org.mule.util.StringUtils.substringAfter(message.payload.Url, 'http://').replace('/','.')]" />
<http:rest-service-component serviceUrl="#[joinChar=message.payload.Url.contains('?')?'&':'?' ; StringUtils.join(new String[]{message.payload.Url,(String)joinChar,'followRedirects=true'})]" httpMethod="GET">
<http:error-filter>
<expression-filter expression="#[Integer.valueOf(message.inboundProperties['http.status']) >= 400]" />
</http:error-filter>
</http:rest-service-component>
<file:outbound-endpoint responseTimeout="10000" doc:name="File" outputPattern="#[flowVars.fileName]" path="/home/user/Documents/output" mimeType="application/pdf"/>
</flow>