Setting Http response data as a varible in Mule4 - anypoint-studio

I am trying to set http response values in variable. Below is response from http request
{
"kind": "drive#file",
"id": "1MxumGPQD9dH161BQJCoJ_",
"name": "2020_August",
"mimeType": "application/vnd.google-apps.folder"
}
How can i set only the id field in a variable after getting above reponse.
Am trying this logic in trans form message
%dw 2.0
output application/json
---
{
"id":payload.id
}
But giving me error

The content that's returned from your HttpRequest call is binary.
Try this:
%dw 2.0
output application/json
---
id: read(payload, "application/json").'id'
which should return what you're looking for:
{
"id": "1MxumGPQD9dH161BQJCoJ_"
}
If you are capturing that into a local variable, define the same using a set-variable, like this:
<set-variable value="#[read(payload, "application/json").'id']" doc:name="id" variableName="id"/>

Related

Can i send raw like form-data in postman?

Can i send raw like form-data in postman? Because i think the server accepts only multipart data which we send via form-data but i need to send the same data via raw.
DATA -
{
"uploadRequest": {
"fileType": "OTHER_EVIDENCE",
"description": "other"
},
"uploadedFile": "cypress/fixtures/evidence.jpeg"
}
ERROR -
{
"httpStatusCode": 500,
"errors": [
{
"code": "1001",
"description": "A service exception has occurred",
"details": "Failed to parse multipart servlet request; nested exception is javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/json"
}
]
}
but if i send this data via form-data it works fine
SUCCESS IMAGE

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

mail.send is not accepting HTML/ CSS code

I have copied the (HTML) source code from a received email just to have the template/format of that email, and this is the error message that I am receiveing when running the API:
Internal : Unexpected error Error during Web API HTTP Request
HTTP Status Code: 400
HTTP Response Content: {
"error": {
"code": "BadRequest",
"message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
"innerError": {
"date": "2020-12-02T17:01:05",
"request-id": "3ab50fc1-0c13-4536-96db-bf6b9b7a736d",
"client-request-id": "3ab50fc1-0c13-4536-96db-bf6b9b7a736d"
}
}
}
I have tested basic commands using HTML such as making text bold and line breaks which work, but anything more complex and I will receive an error.
Update**
Thank you for your response guys, the call that I'm using looks like this:
{
"message": {
"subject": "[Subject]",
"body": {
"contentType": "HTML",
"content": "[Email Body Content]"
},
"toRecipients": [
{
"emailAddress": {
"address": "[Email Address]"
}
}
],
"internetMessageHeaders":[
{
"name":"x-custom-header-group-name",
"value":"Nevada"
},
{
"name":"x-custom-header-group-id",
"value":"NV001"
}
]
}
}
**Another update ** Upon changing the content type to text/html is recieved this error:
Internal : Unexpected error Error during Web API HTTP Request
HTTP Status Code: 415
HTTP Response Content: {"error":{"code":"RequestBodyRead","message":"A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'Microsoft.OData.ODataMediaType, Microsoft.OData.ODataMediaTyp...' matches the content type 'text/plain; charset=utf-8'."}}
note i also have tried text/plain as the content type.
Thank you
Ok, I tried the above payload (just updated the email address) and tested with Microsoft Graph Explorer. It works for me :)
Here's the payload:
{"message":{"subject":"[Subject]","body":{"contentType":"HTML","content":"[Email Body Content]"},"toRecipients":[{"emailAddress":{"address":"test#domain.onmicrosoft.com"}}],"internetMessageHeaders":[{"name":"x-custom-header-group-name","value":"Nevada"},{"name":"x-custom-header-group-id","value":"NV001"}]}}
Here's the snapshot:

Mule Dataweave: How to dynamically map HTTP response(JSON) to XML output

I am building an application, where I have to hit a restful interface, and pass a specific section of the response to the UI.
The response model is quite huge with a lot of fields(String, array of objects, object,number datatypes), so using manual transformation is a big pain.
Is there a way to map the section of the response to a variable and directly send it to the UI.
I tried,
%dw 2.0
%output application/xml
%var UserAcct= payload.UserResponse.UserDetailsResp.UserAccounts
---
{
User: {
"UserAccount": {
Account: UserAcct
}
}
}
This doesn't work because, the payload has List, Array of Objects etc in the response and mapping to a variable throws an error. Is it possible to send the section payload.UserResponse.UserDetailsResp.UserAccounts directly in dataweave?? Please help.
It will be more helpful if you add Input payload, error and XML output.
Following is the code just by assuming your scenario. You can give this a try:
%dw 2.0
output application/xml
---
{
User: {
"UserAccount": {
(payload.UserResponse.UserDetailsResp.UserAccounts map {
Address:{
<XMLFieldName>: $.<respectiveJSONFieldToMap>
....
}
})
}
}
}

Shopify API, unable to create order

I created private application on my test shop and I am trying to create simple order via the Shopify API following the instructions found here:
http://docs.shopify.com/api/order#create
I am using the last example, and sending POST request to admin/orders.json with this in the body of the request:
{
"order": {
"line_items": [
{
"quantity": 1,
"variant_id": 500775053
}
]
}
}
500775053 is a valid variant id.
The response I'm getting is:
{
"errors": {
"line_items": [
"must have at least one line item"
]
}
}
I don't understand what am I doing wrong? Is there some special parameter that I should pass along the request? Some special header?
Thank you for your help!
Try adding these custom headers:
Content-Type: application/json
Accept: application/json