I'm starting a Journey with API Templates, and this is my actual question, but it's answered with "Don't use mapping templates, put logic in client or lambda".
Before even getting that far I'm tried the mapping template from the AWS Blog for Using Amazon API Gateway as a proxy for DynamoDB
In the integration response I paste the text from the blog:
#set($inputRoot = $input.path('$'))
{
"comments": [
#foreach($elem in $inputRoot.Items) {
"commentId": "$elem.commentId.S",
"userName": "$elem.userName.S",
"message": "$elem.message.S"
}#if($foreach.hasNext),#end
#end
]
}
Validation fails with
Validation Result: warnings : [], errors : [Invalid content type specified: #set($inputRoot = $input.path('$')) { "comments": [ #foreach($elem in $inputRoot.Items) { "commentId": "$elem.commentId.S", "userName": "$elem.userName.S", "message": "$elem.message.S" }#if($foreach.hasNext),#end #end ] }]
I tried the answer from this question get the same error.
This was operator error. it was in the error message [Invalid content type specified: I was trying to the conde into the field for content/type.
Related
I am using flask-restx to build an app with a swagger UI and I trying to upload this swagger file as a documentation part in AWS API Gateway. Through this swagger UI, I am enabling the user to upload a CSV file for further data processing.
I have the following swagger json:
{
"swagger": "2.0",
"basePath": "/",
"paths": {
"/upload_profile/csv": {
"post": {
"responses": {
"200": {
"description": "Profile uploaded"
},
"400": {
"description": "Validation Error"
},
"401": {
"description": "Not authorized"
}
},
"operationId": "Get uploaded profiles from user",
"parameters": [
{
"name": "csv_file",
"in": "formData",
"type": "file",
"required": true,
"description": "CSV file"
}
],
"consumes": [
"multipart/form-data"
],
"tags": [
"upload_profile"
]
}
}
},
"info": {
"title": "Upload Profile",
"version": "0.0.1"
},
"produces": [
"application/json"
],
"consumes": [
"application/json"
],
"tags": [
{
"name": "upload_profile",
"description": "Uploading User Profiles"
}
],
"responses": {
"ParseError": {
"description": "When a mask can't be parsed"
},
"MaskError": {
"description": "When any error occurs on mask"
}
}
}
When I go to API Gateway --> Documentation --> Import Documentation and paste the json, I get the following error:
How can the following issue be solved? If formData isn't supported by API Gateway, is there an alternate for hosting the swagger UI?
The problem is that AWS API Gateway expects swagger/OpenAPI version 3, and your file is version 2. If you only want a way to host swagger UI for documentation/collaboration purposes, take a look at SwaggerHub https://swagger.io/tools/swaggerhub/.
But, if you really have to use AWS API Gateway, then you need to get spec in OpenAPI-3 format. Since the API is rather small, I'd suggest preparing OpenAPI-3 spec yourself (rather than generating it) and testing it locally via swagger UI.
I have created AWS API-Gateway and I am trying to upload an image using Postman but I received an error of Multipart: boundary not found.
I have tried below solution for this issue whereas I created model for API as follows:
Model name: fileupload
Content type: image/png
Model schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "UserAccountUpdate",
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"avatar": { "type": "string" },
"username": { "type": "string" }
}
}
}
}
But, this didn't worked.
I expect my image to get uploaded but it receives an error of Multipart: boundary not found.. Please Help me to resolve this issue, Thanks.
Go to the API Gateway settings tab for your API and add "multipart/form-data" to the "binary media types" section:
You have to use HTTP Proxy integration and for Content Handling , choose Passthrough.
I've successfully setup an API that has Kinesis Firehose integration with AWS API Gateway using PutRecord using these instructions (https://aws.mannem.me/?p=1152 - note: it says insecure but I still clicked through since I needed it).
I'm trying to setup an API for PutRecordBatch (essentially allows for more than one record to be written at a time) but I keep getting
{
"__type": "SerializationException" }
Based on hours of research, API gateway throws that error when incoming API call format doesn't match the mapping template noted in the Integration Request. I'm struggling to figure out how to fix my mapping template.
Here's my mapping template:
{
"StreamName": "$input.path('DeliveryStreamName')",
"Records": [
#foreach($elem in $input.path('$.Records'))
{
"Data": "$util.base64Encode($elem.Data)",
}#if($foreach.hasNext),#end
#end
]
}
Here's the test data that I'm sending:
{
"DeliveryStreamName": "test",
"Records": [{
"Data": "SampleDataStringToFirehose"
},
{
"Data": "SampleDataStringToFirehose2"
}]
}
So dumb but the mapping template has an error: there is an extra comma in there at the end of
"Data": "$util.base64Encode($elem.Data)",
that's causing the issue. Below is the correct version:
{
"DeliveryStreamName": "$input.path('$.DeliveryStreamName')",
"Records": [
#foreach($elem in $input.path('$.Records'))
{
"Data": "$util.base64Encode($elem.Data)"
}#if($foreach.hasNext),#end
#end
]
}
Your example helped me a lot so I wanted to complement it, just in case anybody else run into my specific scenario.
In my case, instead of a simple string I needed to send a JSON object, similar to this:
{
"DeliveryStreamName": "test",
"Records": [{
"Data": {"foo": "bar", "count": 321}
},
{
"Data": {"foo1": "bar1", "count": 10}
}]
}
In this case, what happened when I used the template in your example is that the object is stored in a non JSON format, which is not suitable for further analysis.
With a simple adjustment to the template, you could store a correctly formated JSON object:
{
"StreamName": "$input.path('DeliveryStreamName')",
"Records": [
#foreach($elem in $input.path('$.Records'))
{
#set($jsonPath = "$.Records[$foreach.index].Data")
"Data": "$util.base64Encode($input.json($jsonPath))"
}#if($foreach.hasNext),#end
#end
]
}
Through Terraform I have created an AWS apigateway based on a swagger file.
I that swagger file I have added the specific AWS endpoints to document the API, like:
"x-amazon-apigateway-documentation": {
"documentationParts": [
{
"location": {
"type": "API"
},
"properties": {
"description": "This is the API description"
}
},
{
"location": {
"type": "METHOD",
"method": "GET",
"path": "/foo/{bar}"
},
"properties": {
"description": "This is the method description"
}
}
]
}
Then I have published the documentation version 1.0 through AWS console and I am trying to fetch that documentation via HTTP as stated in :
List Documentation Parts
But, unfortunately, I can't. I am doing a GET to the endpoint
http://apigateway.eu-central1.amazonaws.com/restapis/<TheIdOfMyApiGateway>/documentation/parts
And it doesn't even give me a 4XX code but neither response nor HTTP code at all.
What I am doing wrong?
My bad was a typo in the region.
I was missing a dash, the correct endpoint is:
http://apigateway.eu-central-1.amazonaws.com/restapis/<TheIdOfMyApiGateway>/documentation/parts
With eu-central-1 instead of eu-central1
I check the naming of the regions in AWS Regions
I am trying to POST a json string to API Gateway and in turn have API Gateway send the JSON to an EC2 server.
My issue is I can't find good documentation from Amazon on how to accomplish this.
When I test the setup I get this
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Errors><Error><Code>InvalidHttpRequest</Code><Message>The HTTP request is invalid. Reason: Unable to parse request</Message></Error></Errors><RequestID>1fa47f52-d75c-4ff8-8992-3eac11a79015</RequestID></Response>"
Which means very little to me. I assume it is an issue with API Gateway trying to send the request to EC2 and it can't so it generates this error. So perhaps I am setting up the EC2 AWS Service Proxy in API Gateway incorrectly. Which is likely because I have no idea what I am supposed to set 'Action' to right now I have it pointing to the EC2 instance, only cause i don't see any other place to put that info.
This really shouldn't be that hard I have successfully done this thing connecting to Lambda and have looked through all the documentation and all I can find is this: http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html#getting-started-aws-proxy-add-resources
Which is less than helpful for this scenario. Any Ideas?
I think you confused AWS Service Proxy and HTTP Service proxy.
API Gateway can forward API calls to different type of backends:
- a lambda function
- an AWS Service (see http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html for an example)
- an existing API, running on AWS or on premises (your use case)
When defining you API, be sure to define a POST verb and point the Endpoint URL to your EC2 instance URL
I just made a test using the JSON POST service available online at http://gurujsonrpc.appspot.com/ and it works as expected.
Here is the Swagger export of my test API.
{
"swagger": "2.0",
"info": {
"version": "2016-04-11T20:46:13Z",
"title": "test"
},
"host": "c22wfjg4d7.execute-api.eu-west-1.amazonaws.com",
"basePath": "/prod",
"schemes": [
"https"
],
"paths": {
"/": {
"post": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200"
}
},
"uri": "http://gurujsonrpc.appspot.com/guru",
"httpMethod": "POST",
"type": "http"
}
}
}
},
"definitions": {
"Empty": {
"type": "object"
}
}
}