mule 4 : http request from one api to another - correlation id being sent implicitly but not other headers - mule

Am using Mule runtime 4.4
Have two api - api 1 receives request over http and then calls api2 again over http
api 1 receives a GET request with two headers:
X-Correlation-Id = abcd
X-Channel = Music
api 1 then makes a call to api 2 BUT it does NOT send any of the headers
However when request is received in api2 we can see the header X-Correlation-Id = abcd ( unexpected behaviour ? )
However the other header X-Channel = Music is missing ( expected behaviour )
Here is the complete code :
api1 :
<?xml version="1.0" encoding="UTF-8"?>
<mule 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f1bec56d-de65-4a93-913d-07a7426d6bf6" >
<http:listener-connection host="0.0.0.0" port="9091" />
</http:listener-config>
<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="7040eda0-aff5-47c2-8682-be73489e2c2c" >
<http:request-connection host="localhost" port="9092" />
</http:request-config>
<flow name="header_pass_01Flow" doc:id="dc0c28a7-c277-4192-b641-e73af8afbde3" >
<http:listener doc:name="Listener" doc:id="57e56cd6-a47e-456b-8c6d-e57627c673b8" path="/pass/*" config-ref="HTTP_Listener_config"/>
<logger level="INFO" doc:name="Logger" doc:id="6df37e5e-a491-421c-a642-742059107d95" message="before call to api 1"/>
<http:request method="GET" doc:name="Request" doc:id="a6765e5d-40f5-4abd-bff6-d6e6a80eecdf" config-ref="HTTP_Request_configuration" path="/receive/all"/>
<logger level="INFO" doc:name="Logger" doc:id="e250696f-7109-42a4-a50c-586fc164fed9" message="after call to api 1"/>
</flow>
</mule>
api2 :
<?xml version="1.0" encoding="UTF-8"?>
<mule 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="a1f75f77-5ff5-4346-866e-0f1ce1679e57">
<http:listener-connection host="0.0.0.0" port="9092" />
</http:listener-config>
<flow name="header_receive_01Flow" doc:id="433cc316-549d-4349-86ba-68e3571a5716" >
<http:listener doc:name="Listener" doc:id="d3677aa5-bd38-41d4-ba33-cb4a8a1fd2b2" config-ref="HTTP_Listener_config" path="/receive/*"/>
<logger level="INFO" doc:name="Logger" doc:id="6999dea6-c369-4813-a5e6-8e81ec98ef60" message="received call"/>
</flow>
</mule>
How is it that X-Correlation-Id is implicitly being passed ?

The correlation id is the identifier for Mule events, meaning is part of the core functionality of Mule 4. Other attributes or headers are added either by connectors or custom code/operations. Some connectors like the HTTP connector send it by default.

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]"/>

Receiving payload errors when trying to connect to REST 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>

twitter connectorfor multiple user configuration in mulesoft

In Mulesoft, How can I manage multiple twitter user accounts dynamically because at present it requires consumerkey, consumersecret, accesskey and accesstoken in connector configuration for an application which can own only one user.
Please anyone can explain?
<twitter:config name="Twitter__Configuration" accessKey="#[flowVars.accessToken]" consumerKey="#[flowVars.consumerKey]" consumerSecret="#[flowVars.consumerSecret]" doc:name="Twitter: Configuration" accessSecret="#[flowVars.accessTokenSecret]"/>
<flow name="twitterFlow1">
<db:select config-ref="MySQL_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[select * from twittercredentials;]]></db:parameterized-query>
</db:select>
<set-variable variableName="consumerKey" value="#[message.payload[0]['consumerkey']]" doc:name="Variable" />
<set-variable variableName="consumerSecret" value="#[message.payload[0]['consumersecret']]" doc:name="Variable" />
<set-variable variableName="accessToken" value="#[message.payload[0]['accesstoken']]" doc:name="Variable" />
<set-variable variableName="accessTokenSecret" value="#[message.payload[0]['accesstokensecret']]" doc:name="Variable" />
</flow>
<flow name="twitterFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/twitterconnect" doc:name="HTTP"/>
<flow-ref name="twitterFlow1" />
<twitter:show-user config-ref="Twitter__Configuration" doc:name="Twitter"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
assigned the values in variables and accessed as flowvars in the global element which fails.
I think in this post from mule community forum you can find the answer to your question.
Basically you can use mule expression language in twitter globalk connector in this way:
<twitter:config name="Twitter" accessKey="#[flowVars.accessKey]"
accessSecret="#[flowVars.accessSecret]" consumerKey="#
[flowVars.consumerKey]" consumerSecret="#[flowVars.consumerSecret]"/>
Hope this helps
Full example:
This is an example flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:twitter="http://www.mulesoft.org/schema/mule/twitter" 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/twitter http://www.mulesoft.org/schema/mule/twitter/current/mule-twitter.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<twitter:config name="Twitter" accessKey="#[flowVars['accessKey']]" accessSecret="#[flowVars['accessSecret']]" consumerKey="Cannot be parametrized" consumerSecret="Cannot be parametrized" useSSL="false" doc:name="Twitter"/>
<flow name="twitterFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<message-properties-transformer scope="invocation" doc:name="Message Properties">
<add-message-property key="accessKey" value="#['myVarAccessKey']"/>
<add-message-property key="accessSecret" value="#['myVarAccessSecret']"/>
</message-properties-transformer>
<twitter:update-status config-ref="Twitter" status="ciao" doc:name="Twitter"/>
</flow>
</mule>
Here also a screenshot of a wireshark capture using IP filtering (twitter ip families) for debug the http call:

How to Implement Parallel Processing of Message in Mule using VM and Request-Reply Scope

Hi i am working with Mule Any Point Studio and i am curious to know about how this works with VM and Request-Reply scope so that Mule is able to asynchronously process the multiple incoming calls to Mule.
I just want to know how Request-Reply scope works internally. I have gone through complete Mule Tutorial as mention in the given link. But i didn't get the correct idea of working , i am new bee to this.
Request-Reply Scope:
I want to use Request-Reply Scope to implement the Async Parallel Processing with Mule using VM.
For this i went through with this Link but still i need more light on how this works.
Blog:
Here I tried and got the answer of my question with code please have a look at this , i am printing Thread ID that will give clear picture how its working.
<?xml version="1.0" encoding="UTF-8" ?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:ws="http://www.mulesoft.org/schema/mule/ws" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" 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.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/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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<flow name="mule-request-replyFlow1" doc:name="mule-request-replyFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="rr" doc:name="HTTP" />
<request-reply doc:name="Request-Reply">
<vm:outbound-endpoint exchange-pattern="one-way" path="req" doc:name="VM" />
<vm:inbound-endpoint exchange-pattern="one-way" path="res" doc:name="VM" />
</request-reply>
<logger message=" Message payload after Outbound #[message.payload] and Thread is #[Thread.currentThread().getName()] and Thread ID is #[Thread.currentThread().getId()]" level="INFO" doc:name="Logger" />
<object-to-string-transformer doc:name="Object to String" />
</flow>
<flow name="mule-request-replyFlow3" doc:name="mule-request-replyFlow3">
<vm:inbound-endpoint exchange-pattern="one-way" path="req" doc:name="VM" />
<http:outbound-endpoint exchange-pattern="request-response" method="GET" address="http://localhost/service/orderservice" doc:name="HTTP" />
<object-to-string-transformer doc:name="Object to String" />
<logger message="Response Handler Payload is #[message.payload] and Thread ID is #[Thread.currentThread().getId()]" level="INFO" doc:name="Logger" />
<vm:outbound-endpoint exchange-pattern="one-way" path="res" doc:name="VM" />
</flow>
</mule>
The request-reply is just sending the received message to the request endpoint, and then blocking the current thread until a message arrives to the reply endpoint with a correlation id that matches the correlation id that was sent to the request endpoint.
We can summarize it as a synchronous emulator for asynchonous endpoints. In the example you give, you could probably simplify the same leveraging the mule reply to header.

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>