This article demonstrates the issue of stringified data obtained via HTTP and some key points to be considered to obtain a proper JSON response in the output.
What you'll need:
Skill level: Intermediate
Time required: 5 minutes
- HTTP trigger
- Logger
- Any API testing tool such as Postman
Let us take an example to understand the proper execution of HTTP trigger with appropriate payload.
Configuring an HTTP trigger
- Under the Event section, choose the HTTP app from the drop-down menu in the Apps list present right below the New Trigger button.
- Select the New request on HTTP link trigger.
- Specify a random name in the Webhook name field e.g., demo event. This will generate a webhook URL hitting which executes the journey.
- Select HTTP method as POST:
- Specify a JSON body to be returned in the response in the Request payload (Body) field:
{
"field1": "value1",
"field2":"value2",
"field3":"value3"
}
6. Leave the rest of the input fields empty.
Configuring the Logger action
- Under the Steps section, click on the Simple Action button and choose the Logger app from the drop-down menu in the Apps list.
- Select the Log a Message action from the drop-down menu in the Actions list.
- Drag and drop Field1 data pill from the HTTP | New request on HTTP link Trigger under Data Tree Output to the Message input field:
- Now, click the Save & Start button.
Executing the journey
- Copy the HTTP webhook URL and go to the API testing tool of your choice. I have used Postman for testing. Paste the URL in the text field.
- Select the method as POST. Switch to the Body parameter and select JSON as the content type. Now, paste the same JSON body that we have defined in our journey:
- Click the Send button. You’ll get Ok as the response.
- Now, return to the History tab of your journey. In the Output tab of HTTP trigger, you can see that the payload is parsed in JSON format:
- The logger has successfully returned the logged value:
However, JSON responses at times get rendered into a string format. This can be due to the following three reasons:
- A mismatch in a request method
- Inappropriate request header
- Invalid JSON body passed
Let us understand these issues and their solutions in detail.
A mismatch in a request method
Ensure that the request method in an API testing tool is similar to the one defined in the journey. Refer to the following screenshot in the case where two different request methods are used, say POST in the journey and Get in Postman, to call one JSON data:
Here, you’ll be returned with an empty payload and there would be no logged response in the output.
Inappropriate request header
The content type of the JSON body must be application/json. Other content types such as HTML, JavaScript, XM, etc. are not supported. Also, the JSON body parameter type must be raw. Any other parameters such as urlencoded, GraphQL, form-data, etc. are not supported and will return an empty payload.
Invalid JSON data
An invalid JSON data specified in the JSON body either of a journey or an API testing tool results in the stringified output. Refer to the following screenshot:
The output of the HTTP trigger contains string values even though a body is defined in the JSON format. Also, no response is logged.
Rules for specifying the JSON data:
- It should be within the curly bracket {...}.
- The key-value pairs should be enclosed within double quotes "". String values must be defined within double-quotes. E.g., "name":"john".
- All the JSON key-value pairs must end with a comma except the last key-value pair within a JSON object.
- The last curly bracket } of the last JSON object should not have a comma.
Examples of valid JSON data:
One key-value pair |
More than one key-value pair |
{ "name": "john" } |
{ "name": "john", "age":25, "city":"mumbai" } |
Examples of invalid JSON data:
JSON Data |
Reason for being invalid |
{ "name": john } |
No double quotes for string value defined |
{ "name": "john", "age":25 "city":"mumbai" } |
A comma missing for the second last key-value pair |
{ "name": "john", "age":25, "city":"mumbai" }, |
A comma after the closed curly bracket is not accepted |
{ "name": "john", } |
A comma is used since there is only one key-value pair defined in the JSON object |
{ "name": "john", "age":"25", "city":"mumbai", }, |
A comma used for the last key-value pair |
age:23 |
JSON data defined with invalid syntax |
Comments
0 comments
Please sign in to leave a comment.