racket99
racket99

Reputation: 655

How do I avoid putting quotes around each url parameter when using AWS API Gateway?

I am trying to utilize AWS' API Gateway to trigger a lambda function that copies a file from a source bucket to a destination bucket. I would like the form of the API call to be

https://some/api/url/my_lambda_function?key1=joe.mp4&key2=video-files&key3=edited-video-files

I set up the lambda function. I attach an API Gateway and configure the API Gateway. The problem is when I set up the integration mapping.

When I run https://some/api/url/my_lambda_function?key1="joe.mp4"&key2="video-files"&key3="edited-video-files" everything works as it should. However if I run it without the quotes around the parameters, I get an error. For example, if I remove quotes around the key3 parameter, the error is

{"message": "Could not parse request body into json: Unrecognized token \'edited\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: (byte[])\"{\n \"key1\": \"joe.mp4\",\n \"key2\": \"video-files\",\n \"key3\": edited-video-files\n\n}\n\"; line: 4, column: 22]"}

Here's my setup.

Under the API Gateway->Resources->Integration Request->Mapping Templates I click the option (When there are no templates defined). I use application/json and my template is:

{
    "key1": $input.params('key1'),
    "key2": $input.params('key2'),
    "key3": $input.params('key3')

}

For completeness, my Lambda is:

import boto3

def lambda_handler(event, context):
    # initialize s3    
    s3 = boto3.client("s3")

    # print event output
    print(event)

    FILENAME = event['key1']
    SOURCE_BUCKET = event['key2']
    DEST_BUCKET = event['key3']

    # formatted copy string
    copy_source = {
        'Bucket': SOURCE_BUCKET,
        'Key': FILENAME,
    }

    # copy files
    s3.copy_object(Bucket=DEST_BUCKET, Key=FILENAME, CopySource=copy_source)

    return 'Thanks for watching'

Upvotes: 1

Views: 1676

Answers (2)

racket99
racket99

Reputation: 655

It seems to work if I put quotes around the value in the mapping template key-value pairs:

    "key1": "$input.params('key1')",
    "key2": "$input.params('key2')",
    "key3": "$input.params('key3')"

}```

Upvotes: 1

Deiv
Deiv

Reputation: 3097

If you want to pass the url parameters using a key/value pair, eg key1="joe.mp4", then you must use quotes as that defines that key's string value.

However, you can also setup mappings for the URL that don't require the quotes, and are instead separated by a slash ("/") as highlighted in this example, but these aren't as flexible as the key/value setup, because they must be in a specific order.

For example, with a key/value setup you can either do http://url?key1="value1"&key2="value2"&key3="value3", or you can do http://url?key3="value3"&key1="value1"&key2="value2" and it would have the same result (note the order of the keys). However with static parameters separated by slash, you can't do this, all values must be passed in a static order, http://url/value1/value2/value3

Upvotes: 0

Related Questions