I am trying to integrate API gateway with Lambda proxy,
The API server receives the request with these parameters i.e postcode and house
https://api.domain.com/getAddressproxy?postcode=XX2YZ&house=123
However tests from the API gateway to the Lambda proxy does not return values
https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/Test/getaddressproxy?postcode=XX2YZ&house=123
I think the issue is that the lambda function is not passing the query string parameters to the API server.
Any idea how i can pass the query string parameters to the request object?
Code:
from __future__ import print_function
import json
import urllib2
import ssl
print('Loading function')
target_server = "https://api.domain.com"
def lambda_handler(event, context):
    print("Got event\n" + json.dumps(event, indent=2))
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    print("Event here: ")
    print(event['path'])
    print(event["queryStringParameters"])
    req = urllib2.Request(target_server + event['path'])
    if event['body']:
        req.add_data(event['body'])
    # Copy only some headers
    copy_headers = ('Accept', 'Content-Type')
    for h in copy_headers:
        if h in event['headers']:
            req.add_header(h, event['headers'][h])
    out = {}
    try:
        resp = urllib2.urlopen(req, context=ctx)
        out['statusCode'] = resp.getcode()
        out['body'] = resp.read()
    except urllib2.HTTPError as e:
        out['statusCode'] = e.getcode()
        out['body'] = e.read()
    return out
                A REST API can have parameters in at least two ways: As part of the URL-path (i.e. /api/resource/parametervalue ) As a query argument (i.e. /api/resource? parameter=value )
A Python lambda function behaves like a normal function in regard to arguments. Therefore, a lambda parameter can be initialized with a default value: the parameter n takes the outer n as a default value. The Python lambda function could have been written as lambda x=n: print(x) and have the same result.
Create a request in API Gateway ExplorerEnter the details for the request that you wish to execute in the Add Request Configuration dialog (for example: http://localhost:8080/conversion ). If the Request name matches URL setting is not selected, you can supply a custom Request Name for this request.
event["queryStringParameters"] is a dictionary if the API Gateway passes one or None if not passed. Convert this to a query string and append to the Request URL.
...
import urllib
...
qs = urllib.urlencode(event["queryStringParameters"] or {})
req = urllib2.Request(
        ''.join(
          (target_server, event['path'], '?', qs)
        )
      )
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With