bref: Cannot read body content of POST request

v0.3

I try to retrieve the body content of a POST request with an HTTP application configuration

I use this simple handler :

# index.php
<?php 

echo file_get_contents('php://input');

When testing my lambda via Api Gateway everything seems to work fine (when I manually specify the content-length) :

image

But with a « real » HTTP call I cannot read any content from the body of my request…

image


this issue come from this comment : https://github.com/mnapoli/bref/issues/116#issuecomment-453745811

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@mnapoli Works like a charm. Thank you and also @timo-schaefer-sm

@Sh1d0w that’s now fixed thanks to @timo-schaefer-sm

Hello,

I came across the same issue, and could resolved it by forcing content length header on this file : https://github.com/mnapoli/bref/blob/0.3/src/Runtime/PhpFpm.php#L181-L183

if (isset($headers['content-length'])) {
    $requestHeaders['CONTENT_LENGTH'] = $headers['content-length'];
}else if(!empty($requestBody)){
    $requestHeaders['CONTENT_LENGTH'] = mb_strlen($requestBody);
}

PHP binary seems to ignore POST content if content-length header is not set, and this header is forced with this little piece of code. The tricky thing is : why content-length is not forwarded to lambda function ? I suspect a misconfiguration on API gateway stack which block forwarding this header.

@mnapoli perfect everything seems to work fine ! No worries I’m happy to help a little 😉

All that information is super helpful! FYI you can replace the bootstrap of the layer by creating a bootstrap file at the root of your project (and add debug stuff, e.g. by echoing stuff to stdout in the bootstrap).

Here is Bref’s bootstrap for PHP-FPM: https://github.com/mnapoli/bref/blob/0.3/runtime/php/layers/fpm/bootstrap

@mnapoli FYI I have activated detailed logs to retrieve more information.

We can see that body is emptied during the “conversions” process.

Unfortunately Gateway logs are truncated before being sent to cloudwatch (https://forums.aws.amazon.com/thread.jspa?threadID=221740) and I think that we lost important information during the step : Endpoint request body after transformations

Maybe you’ll find something interesting…

(914bad5e-1840-11e9-83f1-efc9a0398ffc) Extended Request Id: TgwN2EJqCYcFnFQ=
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Verifying Usage Plan for request: 914bad5e-1840-11e9-83f1-efc9a0398ffc. API Key: API Stage: m2t7qi7916/Prod
(914bad5e-1840-11e9-83f1-efc9a0398ffc) API Key authorized because method 'ANY /' does not require API Key. Request will not contribute to throttle or quota limits
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Usage Plan check succeeded for API Key and API Stage m2t7qi7916/Prod
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Starting execution for request: 914bad5e-1840-11e9-83f1-efc9a0398ffc
(914bad5e-1840-11e9-83f1-efc9a0398ffc) HTTP Method: POST, Resource Path: /
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method request path:
{}
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method request query string:
{}
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method request headers: {Accept=*/*, CloudFront-Viewer-Country=FR, CloudFront-Forwarded-Proto=https, CloudFront-Is-Tablet-Viewer=false, CloudFront-Is-Mobile-Viewer=false, User-Agent=insomnia/6.3.2, X-Forwarded-Proto=https, CloudFront-Is-SmartTV-Viewer=false, Host=m2t7qi7916.execute-api.us-east-2.amazonaws.com, X-Forwarded-Port=443, X-Amzn-Trace-Id=Root=1-5c3cfa58-7ebfb0ec7e431584f741130e, Via=1.1 17fbe2e6aa5682781b6ee23bfab1fd28.cloudfront.net (CloudFront), X-Amz-Cf-Id=C3SfmopdZJiqADduwSdrD78zLfFxbb5eom3kW3xcZg6d3qpk-PXNeQ==, X-Forwarded-For=82.241.228.91, 54.239.156.98, CloudFront-Is-Desktop-Viewer=true}
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method request body before transformations: coucou=bertrand
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Endpoint request URI: https://lambda.us-east-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-2:894492105031:function:gitlab-webhook/invocations
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Endpoint request headers: {x-amzn-lambda-integration-tag=914bad5e-1840-11e9-83f1-efc9a0398ffc, Authorization=************************************************************************************************************************************************************************************************************************************************************************************************************************1013cf, X-Amz-Date=20190114T210840Z, x-amzn-apigateway-api-id=m2t7qi7916, X-Amz-Source-Arn=arn:aws:execute-api:us-east-2:894492105031:m2t7qi7916/Prod/ANY/, Accept=application/json, User-Agent=AmazonAPIGateway_m2t7qi7916, X-Amz-Security-Token=FQoGZXIvYXdzELb//////////wEaDOmfCIQpl4jVT/tz7yK3A3/hRqJEukqkfs9qCqjohnF5X6OM5YvFHAmJCsWVtDNAniIu5Y+/pf6d9OcEug6DUOx7p28PYiG5k4/Iz/Nj6fySHfGOCG7pSAnSSdyTm9yJYabd4LnIFPXu7/5yK65j/I6Sa9IYD8DbglijvOFG5/E0HGUmMp0aA79dzjaCQ5a0XMFJg88u10afeqt/pWIcq512RvuX0Ll+qlcauJqLK91XaigMez5MseLRMf/BhMu1i1AHhYCqR7mCld06vNQNjLhfZ2dFgQicdGuAUTWvxGTaG3wiLdb80H9depM+gsvQfIX3QoxBbWCjj [TRUNCATED]
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Endpoint request body after transformations: {"resource":"/","path":"/","httpMethod":"POST","headers":{"Accept":"*/*","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":"FR","Host":"m2t7qi7916.execute-api.us-east-2.amazonaws.com","User-Agent":"insomnia/6.3.2","Via":"1.1 17fbe2e6aa5682781b6ee23bfab1fd28.cloudfront.net (CloudFront)","X-Amz-Cf-Id":"C3SfmopdZJiqADduwSdrD78zLfFxbb5eom3kW3xcZg6d3qpk-PXNeQ==","X-Amzn-Trace-Id":"Root=1-5c3cfa58-7ebfb0ec7e431584f741130e","X-Forwarded-For":"82.241.228.91, 54.239.156.98","X-Forwarded-Port":"443","X-Forwarded-Proto":"https"},"multiValueHeaders":{"Accept":["*/*"],"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":["FR"],"Host":["m2t7q [TRUNCATED]
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Sending request to https://lambda.us-east-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-2:894492105031:function:gitlab-webhook/invocations
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Received response. Integration latency: 33 ms
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Endpoint response body before transformations:
{
    "isBase64Encoded": false,
    "statusCode": 200,
    "headers": {
        "x-powered-by": "PHP/7.2.12",
        "content-type": "text/html; charset=UTF-8",
        "status": "200 Ok"
    },
    "body": ""
}
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Endpoint response headers: {Date=Mon, 14 Jan 2019 21:08:40 GMT, Content-Type=application/json, Content-Length=154, Connection=keep-alive, x-amzn-RequestId=914c2240-1840-11e9-afcc-d3d312e75ea6, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5c3cfa58-7ebfb0ec7e431584f741130e;sampled=0}
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method response body after transformations:
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method response headers: {x-powered-by=PHP/7.2.12, content-type=text/html; charset=UTF-8, status=200 Ok, X-Amzn-Trace-Id=Root=1-5c3cfa58-7ebfb0ec7e431584f741130e;Sampled=0}
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Successfully completed execution
(914bad5e-1840-11e9-83f1-efc9a0398ffc) Method completed with status: 200
(914bad5e-1840-11e9-83f1-efc9a0398ffc) AWS Integration Endpoint RequestId : 914c2240-1840-11e9-afcc-d3d312e75ea6
(914bad5e-1840-11e9-83f1-efc9a0398ffc) X-ray Tracing ID : 1-5c3cfa58-7ebfb0ec7e431584f741130e