1

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.

Curiosity
  • 81
  • 6
  • 2
    Please add the HTTP request operation and configuration as XML. – aled Dec 21 '21 at 10:24
  • 2
    Regarding the question added after "EDIT:", it looks to be a new independent question. Please post a separate question for it. – aled Dec 21 '21 at 13:36
  • 2
    "I tried to do it by putting id into URI Parameters, but it is part of URL not URI parameter" URI Parameters are passed as part of the URL. Are you referring to query parameters instead? – aled Dec 21 '21 at 16:49

1 Answers1

2

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
aled
  • 21,330
  • 3
  • 27
  • 34
  • 1
    Sorry, but you misunderstood me. What I am trying to do is to have a variable in Path (something like this `/api/courses/find/#[variable from payload]`), but when I place a variable there it does not work even though logger outputs correct variable value (it displayed correct id in console). What I am asking is how can I now put this variable (id) from payload to Path so HTTP Request I have outputs data from API for this specific id. When I place for example `http://localhost:1234/api/courses/find/60ee9678070e104b2c57be46` in Path it return correct JSON from API. – Curiosity Dec 21 '21 at 14:24
  • 2
    I think I showed how to add a variable part to the request path. You will need to provide more details to clarify the issue. What did not work exactly? Add errors or unexpected values. Show clearly the variable value, and how the request is generated. See the HTTP Wire Logging link I shared to enable it and see requests/responses. Also show what you did in the flow. That will give some context as to what you are trying to do. Think of how we can reproduce the issue. We need all details for that. Read https://stackoverflow.com/help/how-to-ask for tips on how to improve your question. – aled Dec 21 '21 at 15:17
  • 1
    Thank you, you were correct. You helped me truly a great deal, so you have my eternal gratefulness – Curiosity Dec 21 '21 at 19:42