Configuring path parameters in the AWS API Gateway JavaScript SDK

I am trying to set the path parameters when calling the endpoints of the API gateway via the JavaScript SDK and no luck. It seems that I either have something incorrectly configured, or there is an error in generating the SDK.

I can successfully call endpoints that do not accept path parameters, but when I try to pass a parameter that will be used as a path parameter, the SDK just replaces the path parameter with an empty one, and my call fails.

For example, suppose client is a properly initialized API gateway client. I have an endpoint named /measurement with a child of /measurement/{id} . I can call both directly.

client.measurementGet({},{}); - successfully calls my endpoint /measurement client.measurementIdGet({"id": "1234"}, {}); - The browser makes a call /measurement/ instead of /measurement/1234

Looking at the source of my apigClient.js, it looks like the SDK generator does not put the path parameters into the list of parameters it is looking for. For example, the code for my generated measurementIdGet method is as follows:

  apigClient.measurementIdGet = function (params, body, additionalParams) { if(additionalParams === undefined) { additionalParams = {}; } apiGateway.core.utils.assertParametersDefined(params, [], ['body']); var measurementIdGetRequest = { verb: 'get'.toUpperCase(), path: pathComponent + uritemplate('/measurement/{id}').expand(apiGateway.core.utils.parseParametersToObject(params, [])), headers: apiGateway.core.utils.parseParametersToObject(params, []), queryParams: apiGateway.core.utils.parseParametersToObject(params, []), body: body }; return apiGatewayClient.makeRequest(measurementIdGetRequest, authType, additionalParams, config.apiKey); }; 

I dug into assertParametersDefined and parseParametersToObject , and it looks like these methods are expecting a list of parameters to look for. In both cases, the SDK generated empty lists instead of putting its path parameter there.

If I manually updated the generated file to change two lines to

apiGateway.core.utils.assertParametersDefined(params, ['id'], ['body']);

and

apiGateway.core.utils.parseParametersToObject(params, ['id'])

The SDK is making the correct call.

Am I missing something in my configuration or is there an error in the code generator?

+8
amazon-web-services aws-api-gateway
source share
2 answers

If you use a cloud like me. You will need to add it to RequestParameters .

for a resource like this / api / pets / {id} / attributes / {attrid} after running the code

  PetsByIdAttributesByAttridGetMethod: Type: 'AWS::ApiGateway::Method' Properties: RestApiId: !Ref MyApi ResourceId: !Ref PetsByIdAttributesByAttridResource HttpMethod: GET AuthorizationType: AWS_IAM RequestParameters: method.request.path.id : true method.request.path.attrid : true Integration: Type: AWS_PROXY IntegrationHttpMethod: POST Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambda.Arn}/invocations 
+1
source share

Assuming you import a swagger definition to create an API, defining your parameters at the method level as opposed to the path level will cause the generated key filled SDK to work correctly.

 { ... "/path/{to}/resource": { "get": { "parameters": [ // define here "name": "to", "in": "path", ... ], ... }, "parameters": [] // not here } 

Although the definition of parameters at the path level is correct according to the Swagger specification, and the API gateway uses them in the created API, it looks like the API gateway ignores them in some contexts.

0
source share

All Articles