Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

fix #14 send post data to backend when request method is post #17

Merged
merged 1 commit into from
Dec 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ while (true) {
}

if (strlen($body) > 0) {
if($event['httpMethod'] === 'POST'){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works if the content type is application/x-www-form-urlencoded. But I'm not sure it works if the body is instead application/json. The docs make it sound like the body will be incompatible with what CURLOPT_POSTFIELDS expects (see http://php.net/manual/en/function.curl-setopt.php).

Can you verify what happens if you post a JSON payload with Content-Type: application/json set? If this change breaks posting non-form-urlencoded bodies, then please add a condition here that checks the content type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I verified some test case.

Verification code

<?php
$input = file_get_contents("php://input");
echo $input;

verify at local machie

prepare builtin server

php -S localhost:8080 index.php

send request

 curl -X POST http://localhost:8080 -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d '{"key": "val"}'

result

{"key": "val"}

verify at AWS using arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:5

send request

 curl -X POST https://xxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/index.php -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d '{"key": "val"}'

result

empty response body returned

verify at AWS using my layer that patched the issue

send request

 curl -X POST https://xxxxx.execute-api.ap-northeast-1.amazonaws.com/Prod/index.php -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d '{"key": "val"}'

result

{"key": "val"}

I think, It was not send Json Payload from before

I'm not good at English.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Looks great, thanks for confirming the body still comes through!

curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
curl_setopt($ch, CURLOPT_READFUNCTION, function ($ch, $fd, $length) use ($body) {
return $body;
Expand Down