OAuth 2.0 Client Credentials Mulesoft - mule

I am working with mule HTTP request connector's authentication client credentials grant type.
By default it is sending content type as application/json.
Is there a way to override the default content type?
Thanks

You can configure this on the Transform Message compontent before the HTTP request.
Specify the output as such:
<ee:transform doc:name="Transform Message" doc:id="4ed801af-81a5-4072-941f-653c3fbd1666" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/x-www-form-urlencoded
---
{
resource: vars.resource,
client_id: vars.clientId,
client_secret: vars.clientSecret,
grant_type: vars.grantType
}]]>
</ee:set-payload>
</ee:message>
</ee:transform>
Note; You can choose what every output you want on the Tranform Message compontent
Here is a list of all Supported Data Formats

Related

Mule Soft HTTP Request Path from payload

I have created simple flow in which I send id in url like this:
Path:
/api/courses/find/{id}
Result:
http://localhost:88/api/courses/find/60ee9678070e104b2c57be46
then I set this id part as payload. What I am trying to do next is Call REST API with this payload inserted into URL but no matter what I try it does not return correct JSON.
Here is how I need it to look like:
Path:
http://localhost:1234/api/courses/find/60ee9678070e104b2c57be46
Result from Transform Message:
%dw 2.0
output application/json
---
{
isPublished: payload.isPublished,
tags: payload.tags map ( tag , indexOfTag ) -> tag,
"_id": payload."_id",
dishName: payload.dishName,
category: payload.category,
author: payload.author,
ingredients: payload.ingredients map ( ingredient , indexOfIngredient ) -> {
"_id": ingredient."_id",
quantity: ingredient.quantity,
unit: ingredient.unit,
description: ingredient.description
},
cookingTime: payload.cookingTime,
sourceUrl: payload.sourceUrl,
imageUrl: payload.imageUrl,
price: payload.price,
date: payload.date,
"__v": payload."__v",
id: payload."_id"
}
I tried to do it by putting id into URI Parameters, but it is part of URL not URI parameter so that does not work, same goes with something like this:
Request Path on HTTP Request:
/api/courses/find/#[payload]
Does anyone know if I could insert payload inside URL like this? I can't find anything about it in documentation.
Thank you for help!
EDIT: Could you also please tell me if I wanted to create path for each method GET, POST, PUT, DELETE could it have the same Listener path /api/courses and only after that different HTTP Request elements (for example http://localhost:1234/api/courses/ with method POST) or I would have to change it for each one? I am wondering because I can't find if it will know that it is supposed to choose flow based on what HTTP Request elements are in this flow or it just chooses first one with that path /api/courses.
My understanding is that you need to set an URI parameter for an HTTP Request. Please find below an example of a flow that sets a payload with field id and then sets the URI parameter using this field's value. The URI parameter is set in the path attribute of the HTTP Request enclosed in curly braces ({}).
<flow name="testUriParamsFlow" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/"/>
<ee:transform doc:name="Transform Message">
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
{
id: "123456"
}]]>
</ee:set-payload>
</ee:message>
</ee:transform>
<http:request method="GET" path="/api/courses/find/{id}" config-ref="HTTP_Request_configuration" doc:name="Request">
<http:uri-params ><![CDATA[#[output application/java
---
{
id : payload.id
}]]]>
</http:uri-params>
</http:request>
</flow>
Using HTTP Wire logging we can confirm that the request replaces the URI Parameter as part of the URI:
GET /api/courses/find/123456 HTTP/1.1

why i get apikit:router not found 404 error in mule eventhough it requests an already running and deployed api

i have created 2 apis: system and experience for a project. The system had already been deployed into the cloudhub and running successfully. The experience api needs to invoke the system api through a router using the URL:
http://demo-insurance-system-api.us-e2.cloudhub.io
and uriparam is :customer
and queryparams are:?fname=James&lname=Butt
Its working perfectly fine.
but when i want to hit the same url from experience api's requester it gives me
ERROR 2020-05-18 01:58:15,217 [[muleinsurance-exp-api].http.requester.requestConfig.04 SelectorRunner] [event: ] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler:
********************************************************************************
Message : HTTP OPTIONS on resource 'http://demo-insurance-system-api.us-e2.cloudhub.io' failed: not found (404).
Error type : HTTP:NOT_FOUND
Element : muleinsurance-experience-api-main/processors/0 # muleinsurance-exp-api:muleinsurance-experience-api.xml:17
Element XML : <apikit:router config-ref="muleinsurance-experience-api-config"></apikit:router>
(set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
the onpremise testing is done in using postman and the experience url is:
http://localhost:8081/demoapi/customer?fname=James
the experience api raml is:
#%RAML 1.0
title: muleinsurance-experience-api
version: 1.0.0
traits:
client-id-required:
headers:
client_id:
type: string
client_secret:
type: string
responses:
401:
description: Unauthorized, The client_id or client_secret are not valid or the client does not have access.
404:
description: No Record found.
429:
description: The client used all of it's request quota for the current period.
500:
description: Server error ocurred
503:
description: Contracts Information Unreachable.
/demoapi:
/{searchString}:
get:
description: invokes either customer or policy request
queryParameters:
fname:
description: first name
example: Sumitra
required: false
type: string
lname:
description: Last name
example: Ojha
required: false
type: string
dob:
description: Date of Birth
example: 1/2/2003
required: false
type: string
customerID:
description: CustomerID
example: BU79786
required: false
type: string
policytype:
description: type of policy taken
example: Personal Auto
required: false
type: string
responses:
200:
body:
application/json:
example:
{"message": "connecting to System API"}
here i am adding the HTTP request xml snippet:
<choice doc:name="Choice" doc:id="444a5cd6-4aee-441a-8736-2d2fff681e2e" >
<when expression="#[attributes.uriParams.searchString == 'customer']">
<http:request method="GET" doc:name="Request" doc:id="30958d05-467a-41b1-bef3-83426359f2aa" url="http://demo-insurance-system-api.us-e2.cloudhub.io"><http:uri-params ><![CDATA[#[output application/java
---
{ customer : attributes.uriParams.searchString}]]]></http:uri-params>
<http:query-params ><![CDATA[#[output application/java
---
{ fname : vars.fname,
lname : vars.lname,
dob : vars.dob}]]]>
kindly point out where i need to improve. thanks in advance.
The problem is indicated in the error message: HTTP OPTIONS on resource 'http://demo-insurance-system-api.us-e2.cloudhub.io' failed: not found (404).
It looks like your HTTP Request in the experience API is using the HTTP Options method. The RAML indicates that the API only accepts the HTTP GET method. In the XML it should look like: <http:request method="GET" ...

How to do mapping as arraylist of maps for below using dataweave mule

How to do mapping for below data.
ParameterMap{[card=[15242424211], phone=[54545454545]]}
This data is dynamic which is coming from http request as part queryparameters. I want to form query params in the form #[{'p1':'v1', 'p2':'v2'}] dynamically
for example into [{'card': '15242424211','phone':'54545454545'}]
i.e Array of maps(application/java) Using Dataweave in mule
can you please help on this
use following code
%dw 1.0
%output application/json
---
inboundProperties."http.query.params" mapObject {
($$) : $[0]
}
With this output will be {"card": "15242424211","phone":"54545454545"} you can wrap it under array if required by using
%dw 1.0
%output application/json
---
[inboundProperties."http.query.params" mapObject {
($$) : $[0]
}]
This will produce output as [{"card": "15242424211","phone":"54545454545"}]
Please refer org.mule.module.http.internal.ParameterMap for details of HTTP params.
Hope this help.
Update:-
Please use following for setting query parameters for HTTP outbound request.
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
inboundProperties."http.query.params" mapObject {
($$) : $[0]
}
]]></dw:set-payload>
</dw:transform-message>

Storage multipart upload with custom metadata not adding metadata

I'm constructing a multipart/related upload request, as described here, with some custom object metadata in the request body. The upload is successful but the custom metadata fields are not being set.
The request body looks like:
--===============5679188666781658153==
Content-Type: application/json; -charset="utf-8"
MIME-Version: 1.0
{"x-goog-meta-local-path": "./images/02-05-2017/2017-02-05T14:33:30.364112.jpg", "x-goog-meta-capture-ds": "2017-02-05T14:33:30.364112", "name": "0/02-05-2017/2017-02-05T14:33:30.364112.jpg"}
--===============5679188666781658153==
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
<Image Data>
--===============5679188666781658153==--
From my understanding I should be able to arbitrarily set metadata key:value pairs as long as the keys are prefixed with x-goog-meta-*.
Am I missing something? How can I persist the custom metadata to the object using a multipart upload?
I found the answer in this related question: Google Storage API custom header on node.js
As jterrace points out:
Take a look at the JSON request builder here: https://developers.google.com/storage/docs/json_api/v1/objects/insert
You'll notice that metadata is a separate key in the body. So you'll want something like:
var metadata = {
name: "name"
contentLanguage: "en",
metadata: {
"something": "completely different",
},
acl: [...]
};

mule http post json data thru httpendpoint

Using mule - how to post json data to invoke my application url and got response back. I have my json data like below:
{
"Reservation" : {
"reservation" : {
"#id" : "123456789"
},
"arrivingDate" : "03-09-2012",
"departureDate" : "03-15-2012",
"guestName" : "Fred",
"guestLastName" : "Davis",
"hotelID" : "03",
"room" : "1001",
"oceanView" : "true",
}
}
I want post my json data to my application http://localhost:8080/myapplication/createreservation through MULE HTTP ENDPOINT and get some application response back. Any suggestions are welcome.
First thing I like to say is your JSON data is not valid .. there should not be any comma at the end .. may be missing some attributes
If you want to post the Data to an external application you can follow the following example :-
<flow name="BestelItems" doc:name="BestelItems">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<set-payload doc:name="Set Payload" value="{ "Reservation" : { "reservation" : { "#id" : "123456789" }, "arrivingDate" : "03-09-2012", "departureDate" : "03-15-2012", "guestName" : "Fred", "guestLastName" : "Davis", "hotelID" : "03", "room" : "1001", "oceanView" : "true"}}"/>
<http:outbound-endpoint exchange-pattern="request-response" method="POST" address="ttp://localhost:8080/myapplication/createreservation" contentType="application/json" doc:name="HTTP"/>
</flow>
But if you want to post the JSON data to your own Mule application then you only need your flow to have Http inbound endpoint .. and you can post the data from any Rest Client or Postman ..
The data will automatically get into your Mule flow through the rest client or Postman application as below :-
It can be done in the following way:
You can create a POJO with the field names same as the JSON elements
Create an instance of the object and enter your JSON input.
Then use Mule's Object to JSON transformer specifying the source Class with the POJO class.
Invoke the respective 2- way HTTP application using HTTP Outbound endpoint.
Once the response is received, add the JSON to Object transformer and use it to process further.