PUT API with path parameter and request body - json

I have a PUT API Gateway endpoint which should be used to update a DynamoDB entry.
The id for that DynamoDB entry is given as the path parameter.
I have the following mapping template:
"application/json": {
"id": "$input.params('id')",
"title": "$input.json('$.title')"
}
The id is returned as expected.
If I submit this request body
{
"title": "Hello world"
}
I get the error Could not parse request body into json: Unexpected character (\'t\'
Here's the overview of my AWS management console:
what am I missing?

So I just found the solution:
"title": "$input.json('$.title')"
should be
"title": $input.json('$.title') (without the double quotes).
Source: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference

Related

AWS API Gateway VTL Include Source API JSON in Response Template

We would like to include the Integration/Source API JSON in the response, but when we attempt to include the Object, it does not encode properly.
"success": true,
"message": "Resource Call Made Successfully",
"data": {ip=127.0.0.1, ipv4=127.0.0.1, ipv6=0:0:0:0:0:0:0:0, asn=null, asn_name=null, country=US, useragent={family=Other...
As you can see, the strings lose their quotes; breaking the JSON. Here's the template;
#set($inputRoot = $input.path("$"))
{
"success": true,
"message": "Resource Call Made Successfully",
"data": $inputRoot
}
I've tried using $json.parse(), escaping, etc with no luck. Anyone familiar enough to give some direction on how to stringify this so I can include it in the response?
Use syntax like below,
"subject": "$util.escapeJavaScript($input.path('$.Attributes.subject.S')).replaceAll(\"\\\\'\",\"'\")"
It's an explanation in $util.escapeJavaScript() function

Graph API: JSON batching to upload item to OneDrive is failing

I am trying to upload a text file to OneDrive using Graph APIs and I also want to update it in the same request using JSON batch.
My JSON request body is below:
{
"requests":[
{
"id":"1",
"method":"PUT",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y2GOVW772
5BZO354PWSELRRZ:/abc2.txt:/content",
"headers":{
"Content-Type":"application/octet-stream",
"Content-Length":"21"
},
"body":{
"content":"Test content for body"
}
},
{
"id":"2",
"method":"PATCH",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y
2GOVW7725BZO354PWSELRRZ:/abc2.txt",
"headers":{
"Content-Type":"application/json; charset=utf-8"
},
"body":{
"fileSystemInfo":{
"lastModifiedDateTime":"2020-08-09T00:49:37.7758742+03:00"
}
},
"dependsOn":["1"]
}
]
}
When I send this request from my code, I always get a response "Invalid body for request id: 1. The body must be a valid base64 string or JSON.".
Postman refused to run the above request with the message "Method not allowed".
In the above example, I am uploading and updating text files but my code will have to handle all file types (e.g. images, videos, etc.)
Not sure if I am correctly specifying all the JSON fields. Unfortunately unable to find much info on this. Any help would be appreciated.

"status_code": 422, "error_code": "REQUEST_PARAM_PARSE", "detail": "The 'data' parameter must be a list"

I’m using the HTTP Request Wizard to set up a POST request to MonkeyLearn. This endpoint expects a JSON in the request body (or payload body). It must be an object with the data property and a list of the texts you want to classify as value. For example:
{
"data": [
"This is the best sentiment analysis tool ever!!!",
]
}
So, in the HTTP Request Wizard, I set up under Parameters:
Name: data
Value: "This is the best sentiment analysis tool evert!!!"
Type: GetorPost
However, in the preview response I get:
{
"status_code": 422,
"error_code": "REQUEST_PARAM_PARSE",
"detail": "The ‘data’ parameter must be a list"
}
I can’t figure out what list the endpoint expect?
I am not familiar with the "HTTP Request Wizard" and cannot find it either. But from what you've written it is clear that you are sending a string and not an array of strings. You value should look like this: Value: [ "This is the best sentiment analysis tool evert!!!" ]

AWS API Gateway Mapping Template JSON

I've got a API stage that's NOT using "Lambda Proxy integration" which has a Lambda function passing an error.
In the mapping template I have this:
$input.path("$.errorMessage")
Which results in the output of this:
{
"headers": {
"apiVersion": "20190218.1",
"isTesting": true
},
"body": {
"statusCode": 503,
"status": "Service Unavailable",
"title": "One or more of our data providers are currently offline for scheduled maintenance"
}
}
The header values are mapped to template headers and pull through correctly, however I need the body to transform to this:
{
"statusCode": 503,
"status": "Service Unavailable",
"title": "One or more of our data providers are currently offline for scheduled maintenance"
}
Whatever I have tried, body always returns as a blank string, an empty body, or an invalid JSON.
This is the closest I've got but it returns an invalid JSON:
$util.parseJson($input.path("$.errorMessage")).body
Result (comes back with no quotes):
{statusCode=503, status=Service Unavailable, title=One or more of our data providers are currently offline for scheduled maintenance}
Is it possible to do what I'm after? I can't find a reverse for $util.parseJson (i.e, stringify).
Thanks!
I think the original poster has probably moved on in the past 11 months, but in case anyone else stumbles across this question, $input.json('$.errorMessage.body') should work.

Twilio HTTP response: Not parsing XML/JSON

Using Twilio Studio, my HTTP widget obtains the following JSON from (external) endpoint:
{
"response": {
"say": "1 address found",
"data": [
"10707 Ewing Drive Kansas City Kansas"
]
}
}
However, I am able to access {{widgets.http_address_schedule.body}} but not {{widgets.http_address_schedule.parsed.say}}. Twilio documentation indicates this is possible if my response is JSON.
From documentation at: https://www.twilio.com/docs/studio/user-guide#rest-api
Json: If your function returns valid Json, you should be able to
access it via widgets.MY_WIDGET_NAME.parsed
For example, if you return {"message": "Hi", "person": {"name": "Bob",
"age": 40}}, you can reference that in subsequent widgets as:
widgets.MY_WIDGET_NAME.parsed.message
widgets.MY_WIDGET_NAME.parsed.person.name
widgets.MY_WIDGET_NAME.parsed.person.age
What is the expected way to send data to be parsed by Twilio?
After reviewing Twilio Studio logs, I was able to identify PHP error output embedded in the JSON response. Once the error was resolved Twilio was able to successfully parse response and provide data in parsed key in {{widgets.http_address_schedule.parsed.response.say}}