aws / aws/aws-sam-cli

sam local start-api event pathParameters and path difference compared to API gateway

Open
#3,524 3 comments 1 reaction 0 assignees View on GitHub
area/local/start-api stage/needs-investigation
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

Hi.

I have a strange problem.
We are developing a rest API using AWS sam, API gateway, and lambdas.

We encountered the following problem.
Let's say we have a resource and we want to access that, with the method of providing the id in the URL:

- https://..../resource/my-id-1

For this example, everything is fine, we define the template as follows:
```
Type: AWS::Serverless::Function
Properties:
Events:
GetResource:
Type: Api
Properties:
RestApiId: !Ref Broker
Path: /resource/{ID}
Method: get
```

But now we have ids with a slash character in it.
Ok, no problem, we just simply encode it when calling, decode it when handling it.

Let's say the new id is the following: **my-id/100**

Now let's call the following URL to achieve what we want:
- https://..../resource/my-id%2F100

**_! Note that we already encoded the id!_**

This is when we encounter the difference between local testing and production.

When we check the event in production, the event is as expected:
```
{
"pathParameters": {
"ID": "my-id%2F100"
},
...
}
```

The behaviour after executing "sam local start-api", and sending the HTTP request is that the function does not event get invoked.
It feels like it decodes the URL first (which the API gateway does not do by default as can be seen from the example), and it tries to find an event with /resource/{ID}/{SOMETHING}, but there is no such.

I know this is only a little inconvenience when developing, everything works fine in production, but is there any fix for this?
---
Tested with sam cli 1.25.0 and 1.36.0, no difference at all.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.