Reza Amya
Reza Amya

Reputation: 1724

How use segmented URL in AWS API Gateway?

I have a Lambda Function that it is accessible by an API Gateway. I can handle all POST and GET submitted requests to API endpoint (https://XXXXXXX.execute-api.us-east-1.amazonaws.com/default/myapi) inside my Lambda, but I need to use some segments at end of my URL when I am using PUT requests.

My Python code to call the API is here and it is working correctly:

import requests
import json

url = 'https://XXXXXXX.execute-api.us-east-1.amazonaws.com/default/myapi'
token = "my token"

data = {
        "first_name": "Reza",
        "birthday": "1986-09-12"
        }

headers = {"Content-Type" : "application/json", "x-api-key":"MY_API_KEY"}
response = requests.put(url, data=json.dumps(data), headers=headers)
print(response.text)

But if I add users segment to end of the URL like this:

url = 'https://XXXXXXX.execute-api.us-east-1.amazonaws.com/default/myapi/users'

it will show this error:

{"message":"Missing Authentication Token"}

I need to add some static segments like users to return the list of all users and some dynamic segments like users/USER_ID (when USER_ID is a dynamic number) to return the information for a special user.

can you please guide me how I can use segmented URL in my AWS API Gateway?

Upvotes: 1

Views: 793

Answers (1)

Kannaiyan
Kannaiyan

Reputation: 13055

The term you are using segmented URL might have caused your confusion. It is called path parameters with AWS. There is more than one way to do it. ANY+ integration is the easiest to handle.

Integrate with ANY+ integration to your lambda and you are good to go. All the path parameters will be delivered to your lambda.

http://www.1strategy.com/blog/2017/06/06/how-to-use-amazon-api-gateway-proxy/

Additional path parameter documentation,

https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html#api-as-lambda-proxy-expose-get-method-with-path-parameters-to-call-lambda-function

Good luck.

Upvotes: 2

Related Questions