Skip to content

POST/PATCH request via file_get_contents + stream_context_create switches to GET after a HTTP 308 redirect #11274

Closed
ThePHPF/thephp.foundation
#90

Description

@ThiefMaster

Description

The following code:

<?php
echo file_get_contents('http://127.0.0.1:8080/test', false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
echo file_get_contents('http://127.0.0.1:8080/test', false, stream_context_create(['http' => ['method' => 'PATCH', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
echo file_get_contents('http://127.0.0.1:8080/test/', false, stream_context_create(['http' => ['method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));
echo file_get_contents('http://127.0.0.1:8080/test/', false, stream_context_create(['http' => ['method' => 'PATCH', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(['hello' => 'world'])]]));

Resulted in this output:

{"method":"GET"}
{"method":"GET"}
{"hello":"world","method":"POST"}
{"hello":"world","method":"PATCH"}

But I expected this output instead:

{"hello":"world","method":"POST"}
{"hello":"world","method":"PATCH"}
{"hello":"world","method":"POST"}
{"hello":"world","method":"PATCH"}

The server on 127.0.0.1:8080 is this small Flask app (I'm not really a PHP person anymore so I couldn't be bothered to use PHP for this after not having written any PHP code in the last 15 or so years ;)):

from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/test/', methods=['GET', 'POST', 'PATCH'])
def test():
    return jsonify(method=request.method, **request.form)

app.run(port=8080)

When sending a request to the URL without the trailing slash, it uses a HTTP 308 redirect to redirect to the URL with that slash.
Clients are required to keep the method and payload when encountering such a redirect:

See MDN:

The request method and the body will not be altered, whereas 301 may incorrectly sometimes be changed to a GET method.

And even RFC7538 specifying this status code is pretty clear about the expected behavior from clients:

Note: This status code is similar to 301 (Moved Permanently) ([RFC7231], Section 6.4.2), except that it does not allow changing the request method from POST to GET.

PHP Version

8.2.6

Operating System

No response

Activity

  1. ThiefMaster commented on May 19, 2023

    @ThiefMaster
    Author

    I think the relevant code causing the bugs is here:

    Ideally redirects with 307 or 308 status codes would NOT trigger this logic, while other redirects stick with the current behavior (for 301/302 it's generally assumed and expected that the method changes to GET).

  2. ndossche commented on May 19, 2023

    @ndossche
    Member

    Yes, and there's a few locations mores where the issue lies. I started working on a fix.

  3. self-assigned this
    on May 19, 2023
  4. added a commit that references this issue on May 19, 2023
    dc7ffd5
  5. added 2 commits that reference this issue on May 19, 2023
    45897f5
    afcf97b
  6. added a commit that references this issue on Feb 3, 2024
    1ede313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions