Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ETH-3929 omit body from GET and DELETE requests #21

Merged
merged 3 commits into from
Apr 19, 2023

Conversation

00dani
Copy link
Contributor

@00dani 00dani commented Apr 19, 2023

While it's legal by the standard to include a request body on any kind of HTTP request, it's not normally done with methods like GET and DELETE, and so not all HTTP frameworks properly support GET requests with bodies included. Rudely.

Rather than trying to pass up a request body for these methods, we convert the body to query parameters and use those instead. Not too complicated.

It's technically legal for GET and DELETE requests to include a request
body, but some frameworks don't bother to support them and will reject
the request. Rude.

Query parameters are supported for all types of requests though, so we
can convert the body to that format when necessary and provide no
request body. Not too tricky.
@00dani
Copy link
Contributor Author

00dani commented Apr 19, 2023

Note that this was written against the PHP7 compatible version of sdk-php, since that's the version api-app and friends need. I've confirmed it merges cleanly with current mainline as well, however.

Comment on lines +125 to 133
$verb = strtoupper($verb);
$url = new Uri(Router::getRouteUrl($route));

if (in_array($verb, ['POST', 'PUT', 'PATCH'])) {
$body = json_encode($body, JSON_THROW_ON_ERROR);
} else {
$url = $url->withQuery(http_build_query($body));
$body = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

already approved as this is a non blocking comment, only a suggestion to clean it up for readability:

maybe avoid using if else, e.g. swap to private methods with early returns or however you want to do it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's an idea! I do enjoy early returns. I'll experiment a little and see if I can make things a bit cleaner. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ended up deciding it wouldn't be any simpler with a separate method, since it needs to change both the $body and potentially the $url and that would mean fussing around with multiple return values.

Doable, of course, but not exactly something PHP enjoys or handles very well.

@00dani 00dani merged commit 045270d into v4 Apr 19, 2023
@00dani 00dani deleted the feature/ETH-3929-omit-get-body branch April 19, 2023 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants