question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

REST API: Request failed with status code 431

See original GitHub issue

Describe the bug I have an aws-serverless-express lambda function, created by Amplify, that is invoked by an Amplify created API. The API was configured to not require authorization. However, when using the Auth module, it appears that the x-amz-security-token is put in both multiValueHeaders and headers, in the lambda event object. Example structure (notice “headers” and “multValueHeaders”):

{
    "resource": <OMITTED>,
    "path": <OMITTED>,
    "httpMethod": "GET",
    "headers": {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "br, gzip, deflate",
        "Accept-Language": "en-us",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "US",
        "Host": <OMITTED>,
        "User-Agent": "aws-amplify/1.0.25 react-native",
        "Via": <OMITTED>,
        "X-Amz-Cf-Id": <OMITTED>,
        "x-amz-date": "20190501T155047Z",
        "x-amz-security-token": <SECURITY_TOKEN>,
        "X-Amzn-Trace-Id": "Root=1-5cc9c057-73b3fb6ac5775296d659372c",
        "X-Forwarded-For": "24.243.147.62, 70.132.9.87",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "multiValueHeaders": {
        "Accept": [
            "application/json, text/plain, */*"
        ],
        "Accept-Encoding": [
            "br, gzip, deflate"
        ],
        "Accept-Language": [
            "en-us"
        ],
        "CloudFront-Forwarded-Proto": [
            "https"
        ],
        "CloudFront-Is-Desktop-Viewer": [
            "true"
        ],
        "CloudFront-Is-Mobile-Viewer": [
            "false"
        ],
        "CloudFront-Is-SmartTV-Viewer": [
            "false"
        ],
        "CloudFront-Is-Tablet-Viewer": [
            "false"
        ],
        "CloudFront-Viewer-Country": [
            "US"
        ],
        "Host": [
            <OMITTED>
        ],
        "User-Agent": [
            "aws-amplify/1.0.25 react-native"
        ],
        "Via": [
            <OMITTED>
        ],
        "X-Amz-Cf-Id": [
            <OMITTED>
        ],
        "x-amz-date": [
            "20190501T155047Z"
        ],
        "x-amz-security-token": [
             <SECURITY_TOKEN>
        ],
        "X-Amzn-Trace-Id": [
            "Root=1-5cc9c057-73b3fb6ac5775296d659372c"
        ],
        "X-Forwarded-For": [
            "24.243.147.62, 70.132.9.87"
        ],
        "X-Forwarded-Port": [
            "443"
        ],
        "X-Forwarded-Proto": [
            "https"
        ]
    },
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "resourceId": <OMITTED>,
        "resourcePath": <OMITTED>,
        "httpMethod": "GET",
        "extendedRequestId": <OMITTED>,
        "requestTime": "01/May/2019:15:50:47 +0000",
        "path": <OMITTED>,
        "accountId": "<OMITTED>,
        "protocol": "HTTP/1.1",
        "stage": "dev",
        "domainPrefix": "4xnuilt9x4",
        "requestTimeEpoch": 1556725847460,
        "requestId": <OMITTED>,
        "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "sourceIp": <OMITTED>,
            "accessKey": null,
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "aws-amplify/1.0.25 react-native",
            "user": null
        },
        "domainName": <OMITTED>,
        "apiId": <OMITTED>
    },
    "body": null,
    "isBase64Encoded": false
}

To Reproduce Steps to reproduce the behavior:

  1. amplify add auth (fine-grained multi-access IAM)
  2. amplify add api (create a serverless lambda function with no authentication required)
  3. configure the lambda function in the AWS console, to use a custom runtime (node 12).
  4. set a layer to ARN to arn:aws:lambda:<region>:553035198032:layer:nodejs12:1
  5. call the api using API.get(<API>, "/path") and you might get an error like the one below:
Error: Request failed with status code 431
    at createError (createError.js:16)
    at settle (settle.js:18)
    at XMLHttpRequest.handleLoad (xhr.js:77)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:572)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:394)
    at XMLHttpRequest.js:507
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:366)
    at MessageQueue.js:106

Expected behavior Just a few days ago, I was able to make these sort of API calls, and after removing and replacing the API with a slightly different config (no dynamodb dependency in the lambda function), I get 431 on all my calls.

Desktop (please complete the following information):

  • OS: mac osx
  • Browser: chrome

Smartphone (please complete the following information):

  • Device: iOS Simulator iPhone X
  • OS: iOS 12.0 (i think)

Sample Code index.js (had to be slightly altered form default, to run with node 11+)

const app = require("./app");

const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => {
  console.log(EVENT: ${JSON.stringify(event)}`);
  console.log(Buffer.byteLength(JSON.stringify(event)));
  awsServerlessExpress.proxy(server, event, context);
};```

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
potatopotarocommented, May 1, 2019

Aha!! I was trying to set NODE_OPTIONS="--max-http-header-size=16000" under process.env without any luck, however setting the variable in the aws console for lambda works. You da man! Thanks!

0reactions
github-actions[bot]commented, Jun 12, 2021

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP Error 431: 3 Ways to Fix Request Header Fields Too ...
The HTTP Error 431: Request Header Fields Too Large response status code indicates that the origin server refuses to fulfill users' requests.
Read more >
How To Fix HTTP Error 431 Request Header Fields Too Large
Learn how to fix the HTTP Error 431 Request Header Fields Too Large message using four simple troubleshooting tips.
Read more >
the server responded with a status of 431 (Request Header ...
431 HTTP response status code is sent from the server when client's HTTP Header is greater than the server's accepting HTTP Header ......
Read more >
HTTP Error 431: Definition, Status, Causes & Solutions | Okta
Error 431, which officially says "Request header fields too large," means the server has dropped the request. The header sent by the user...
Read more >
431 Request Header Fields Too Large - TooBigHeaders
This error occurs if the total size of all the request headers sent by the client application to Apigee Edge as part of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found