Search code examples
phpapiresthttprequestput

How to get the data from PUT HTTP Request (form-data)


This is my function in my API I need to get the data from the form-data from the HTTP request.

I need to get the put values to update my data in my class query.

    function method()
    {
        $method = $_SERVER['REQUEST_METHOD'];

        return $method;
    }

    function updateUser()
    {
        $method = method();

        if($method == 'PUT'){

            $put_data = file_get_contents("php://input");

            parse_str($put_data, $post_vars);


            return $post_vars;


        }else{

            $status = '400 Bad Request';

            return $status;

        }
     }

{"------WebKitFormBoundaryYKcobRh4FtrGCYaI\r\nContent-Disposition:_form-data;_name":"\"test\"\r\n\r\ntest_value\r\n------WebKitFormBoundaryYKcobRh4FtrGCYaI--\r\n"}

This is what i get from the return $post_vars

I need to have test: test_value


Solution

  • Change the encoding of the request from multipart/form-data to application/x-www-form-urlencoded