How to set full url dynamically in http request connector in Mule - http

As shown in this question, it is possible to have parts of the url in variables, when requesting an http endpoint with mule.
But I have a full url, returned from an endpoint supporting hateoas. Is it possible to use this full url for requests without splitting it up into several parts (host, port, path)?
With full url I mean sth. like "http://example.com/a/specific/path" instead of host="example.com", port="80", path="/a/specific/path"

One simple way to do is to split the url into multiple parts and set it into a flow variables. Then you can use this flow variables anywhere you want.
An simple example to do it is as follows:-
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="csv-to-smtpFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/aa" doc:name="HTTP"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="http://example.com:80/a/specific/path" doc:name="Set Payload"/>
<set-variable variableName="url" value="#[new URL(payload);]" doc:name="Variable"/>
<set-variable variableName="protocol" value="#[url.getProtocol();]" doc:name="Variable"/>
<set-variable variableName="host" value="#[url.getHost();]" doc:name="Variable"/>
<set-variable variableName="port" value="#[url.getPort();]" doc:name="Variable"/>
<set-variable variableName="path" value="#[url.getPath();]" doc:name="Variable"/>
<logger message="Done" level="INFO"/>
</flow>
Here you can see I am setting the url http://example.com:80/a/specific/path in a payload and then splitting it into host, port, path etc and storing it in variables.
Pls note if your url is in the form http://example.com:80/a/specific/path, you will get the port using expression #[url.getProtocol();]
But if your url is in the form http://example.com/a/specific/path, you will not get the port number.
Ref:- https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

Related

Mulesoft Rest Web service

I creating Rest Web service for fetching attachment file through http and storing file to local drive so i need filename dynamically as it is come in http request
My flow as follows
<foreach doc:name="For Each">
<set-payload value="#[payload.getInputStream() ]" doc:name="Get Inputstream from Payload"/>
<logger message="}}}#[message.inboundAttachments['payload']]" level="INFO" doc:name="Logger"/>
<file:outbound-endpoint path="F:\mule_data\e_files" responseTimeout="10000" doc:name="File" outputPattern="abcd.jpg"/>
</foreach>
<set-payload value="{"status":"success"}" doc:name="Generate JSON Response" />
</flow>
Here outputPattern="abcd.jpg" is harcoded so i need filename and format which coming through request
Please help
i refer from https://blogs.mulesoft.com/dev/mule-dev/soap-rest-attachments/
Please go through my answer in the question linked below.
How send a file in an HTTP request and upload it to file server via FTP in Mule
Once you are able to read/get the attachment file,you can get all the metadata associated with file.For reading name you can do following-
use #[payload.dataSource.part.fileName] to get name of attachment.

Mule: How to dynamically set a url on an http request endpoint

Using the deprecated http implementation it was possible to dynamically set the url on an outbound http endpoint from the payload or properties:
<http:outbound-endpoint address="http://#[payload]" method="GET" />
Is it possible to do this using the new http request connector?
Yes, it is. Here is a simple example:
<http:request-config
name="HTTP_Request_Configuration"
host="#[flowVars.address]"
port="80"
basePath="/"
doc:name="HTTP Request Configuration"/>
<flow name="httpFlow">
...
<set-variable
variableName="address"
value="#[message.inboundProperties.'http.query.params'.site]"
doc:name="Set site address variable"/>
<http:request
config-ref="HTTP_Request_Configuration"
path="/"
method="GET"
doc:name="Get dynamic HTTP"/>
</flow>
Just define the host attribute using the MEL expression you require.
host="#[flowVars.someVariable]"

Mule HTTP parameter

I created a service where you call the mule service via http:
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8086" path="idnum" doc:name="HTTP"/>
so if you want to call this service you type:
http://localhost:8086/idnum
but what i want is for the http to accept an id number as a parameter and then store the id number into a variable so that i can use the id number. So the http would look like:
http://localhost:8086/idnum/4583948364094 for example.
So my question is how do you get the parameter from the url
From Mule-3.6 on wards, we have HTTP Listener Connector, using which you can pass URI parameters.
You can access the URI parameters using the below MEL
#[message.inboundProperties.'http.uri.params'.id] provided your URI should be like this: http://localhost:8086/idnum/{id}
You need to put the id as a message inbound parameter as following .. In Mule flow you need to do the following :-
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8086"
path="idnum" doc:name="HTTP"/>
<!-- storing id in variable myId -->
<set-variable variableName="myId"
value="#[message.inboundProperties['id']]"
doc:name="Variable"/>
<!-- Print the variable in console -->
<logger level="INFO" message="My id :- #[flowVars['myId']]"/>
</flow>
Now in browser access
http://localhost:8086/idnum/?id=4583948364094
Now you can store it in a variable in Mule and you can see it in a logger like:
My id :- 4583948364094

403 Forbidden error Mule using http outbound endpoint

I am trying to poll an RSS feed using Mule Studio but have been unsuccessful polling the information using the http outbound-endpoint in the Mule application. I have previously encountered the same issue using the http inbound-endpoint with the same site but was able to resolve the issue by assigning the User-Agent to MuleESB by setting the address to be
http://www.theaggie.org/feed/?User-Agent=MuleESB
However, now with the http outbound-endpoint, I am unable to poll the feed and get a 403 Forbidden error. My XML for the flow is
<flow name="aggregatorFlow1" doc:name="aggregatorFlow1">
<poll>
<processor-chain>
<set-variable variableName="httpMessages" value="#[[]]" />
<http:outbound-endpoint exchange-pattern="one-way" address="http://www.theaggie.org/feed/?User-Agent=MuleESB" method="GET" />
<expression-component>httpMessages.add(message.payloadAs(java.lang.String))</expression-component>
</processor-chain>
</poll>
<logger level="INFO" message="#[httpMessages]" />
</flow>
If I change the exchange-pattern to request-response, the logger would just output [ ]. I do not have issues with other sites using the same xml code.
Clearly the exchange-pattern should be request-response, as you seem to care about the payload of the response.
Try adding the following before the http:outbound-endpoint:
<set-property propertyName="User-Agent" value="MuleESB" />

Is there a way to poll several addresses using Mule's HttpPollingConnector?

I am trying to poll several addresses (URL links) from a list that contains all these addresses using the http polling connector in Mule. Currently, I am only able to poll from one address but I would like to find a way to use this list to iterate the polling for each site. Is there anything built within Mule that provides such function?
Is composite source what you're looking for? It allows you to have more than one endpoint in the inbound.
e.g. from http://www.mulesoft.org/documentation-3.2/display/32X/Bookstore+Example
<flow name="CatalogService">
<composite-source>
<!-- Public interface -->
<inbound-endpoint address="http://0.0.0.0:8777/services/catalog" exchange-pattern="request-response">
<cxf:jaxws-service serviceClass="org.mule.example.bookstore.CatalogService" />
</inbound-endpoint>
<!-- Administration interface -->
<inbound-endpoint address="servlet://catalog" exchange-pattern="request-response">
<!-- Convert request parameters to Book object -->
<custom-transformer class="org.mule.example.bookstore.transformers.HttpRequestToBook" />
<response>
<!-- Format response to be a nice HTML page -->
<custom-transformer class="org.mule.example.bookstore.transformers.AddBookResponse" />
<!-- Force text/html, otherwise it falls back to request
props, which have form-encoded one -->
<transformer ref="setHtmlContentType" />
</response>
</inbound-endpoint>
</composite-source>
....
Edit:
The following is a simple foreach example:
<flow name="foreachFlow1" doc:name="foreachFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<foreach collection="#[groovy:['localhost:8082', 'localhost:8083']]" doc:name="For Each">
<http:outbound-endpoint exchange-pattern="request-response" address="http://#[payload]" method="GET" doc:name="HTTP"/>
</foreach>
</flow>
<flow name="foreachFlow2" doc:name="foreachFlow2">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
<logger message="in flow2" level="INFO"/>
</flow>
<flow name="foreachFlow3" doc:name="foreachFlow3">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8083" doc:name="HTTP"/>
<logger message="in flow3" level="INFO"/>
</flow>
Basically, the 'tricky' part is to figure out that the payload becomes the current item in the collection you're iterating over (the docs do a wonderful job at pointing that out... well at least if you're into fishing :P).

Resources