Home > Blockchain >  When I fetch using GET method I receive an error 502
When I fetch using GET method I receive an error 502

Time:02-02

When I try to fetch using GET I recieve an error 502 but when I fetch using POST i recieve the response.

Lambda:

const headers = {'Content-Type':'application/json'}


exports.handler = async function(event) {
    return {
      statusCode: 200,
      headers: {
        headers
      },
      body: JSON.stringify({"ok":"ok"})
    }
};

Fetch:

fetch("https://n3bvznv385.execute-api.us-east-2.amazonaws.com/dev/juegos", {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'authorizationToken' : '123',
            }
        }).then(response => response.json())
            .catch(error => console.error('Error:', error))
            .then(response => console.log(JSON.parse(response.body)));

Error using GET Success using POST

CodePudding user response:

API Gateway expects the following response format from your Lambda when you are using Proxy Integration:

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "multiValueHeaders": { "headerName": ["headerValue", "headerValue2", ...], ... },
    "body": "..."
}

You are, however, passing a nested object as headers as your code resolves to this:

headers: { {'Content-Type':'application/json'} }.

Try to include possible headers like this instead:

exports.handler = async function(event) {
    return {
      statusCode: 200,
      headers: headers,
      body: JSON.stringify({"ok":"ok"})
    }
};
  •  Tags:  
  • Related