0

Context

I am implementing a user information update using a PUT request in Laravel 8. I use Postman to send the request and see the results.

Expected behavior

My PUT request reaches the controller's function that is censed to update the authenticated user. The latter is updated successfully. So the validate call is executed succesfully and finds the data in the request.

Actual behavior

My PUT request reaches the controller's function that is censed to update the authenticated user. The latter is not updated successfully. In fact, the validate call is executed succesfully but doesn't find the data in the request.

Instead, data validation says:

{ "message": "The given data was invalid.", "errors": { "email": [ "The email field is required." ], "name": [ "The name field is required." ] } }

The route & The request

Postman request

curl --location --request PUT 'https://XYZ/api/set_user_data' \
--header 'X-Requested-With: XMLHttpRequest' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer 12|leRLA5yopCLIxe0oN9MMThctqD78iJDjZdZQkcgs' \
--data-urlencode '[email protected]' \
--data-urlencode 'name=test2'

It means in Postman terminology that no "Params" are sent, an Authorization Bearer token is sent, some Headers are sent and some Body's x-www-form-urlencoded data are sent.

API Route

In routes/api.php:

Route::put('/set_user_data', [UserController::class, 'setUserData'])->name('set_user_data');

UserController::setUserData:

public function setUserData(Request $request) {
    if(!Auth::check()) {
        return 'unauthenticated.';
    }

    $request->validate([
        'email' => 'required|email',
        'name' => 'required|string'
    ]);

 // ... update user here but out of topic
}

What I tried to do... or to not do

  • Some Stackoverflow answers are: send a POST request and send in the body _method=PUT. I don't want to do this. I really prefer to send a PUT request. Because I am developing an API. It totally justifies the fact that I must use a PUT request and not a PUT one.

  • Some Stackoverflow answers are: use x-www-form-urlencoded not a simple form. It doesn't fix the problem; moreover it's already the case. Maybe it could help with images sending. (notice I don't want to send any image here).

Question

Why Laravel's validate don't find the data of my request and how to fix it?

1
  • you need to send a valid JSON request. Laravel only requires method PUT on the web requests, not on API requests. Commented Mar 12, 2021 at 9:10

1 Answer 1

1

You are sending the request as "Content-type: application/json", but you are not sending the body as valid JSON.

You are sending this:[email protected]&name=test2

You should send this:

{"email":"[email protected]", "name": "test2"}

a valid JSON object

Sign up to request clarification or add additional context in comments.

4 Comments

You think it's why validate doesn't execute?
@JarsOfJam-Scheduler i think validate is executing, it is just not seeing the data because of the body sent not as JSON
You are right ty! And if one day I needed to send an image using json, for example I could send the base64 version?
For those who want to know how I send the PUT request with JSON data in Postman, well I just click on "Body" then "raw" then I selected "JSON" and I've written the data at JSON format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.