Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shashi_verma's avatar

How to show JSON response after changing the HTTP method or Request type in postman?

I have an API, and there is a login method with POST request type, so when I hit the API path in postman with POST request type it works fine, but when I change the request type to GET it shows an error that shows "No message", how can I show JSON response here?

0 likes
6 replies
takdw's avatar

Well, your API should have an endpoint that 'listens' for GET requests to the /login route. You can send a response from that endpoint.

Route::get('/login', function() { /** send response */ }); / but this is usually a login page endpoint
Route::post('/login', function() { /** login logic */ });
takdw's avatar

Hmm... Do you want to send an error response when the app/users hit an endpoint that is not available (or if they access it using the wrong RequestType)? If so, you can add a general catch rule for requests that do not match any other rules. It will take the form of something like this:

routes/api.php or routes/web.php

// All your other rules
Route::post('/login', 'AuthController@store');
// ...

// Anything else, throw a 404 error
Route::get('/{any}', function () {
    $data = [
        'error' => [
            'message' => 'Unknown endpoint'.
            'statusCode' => 404
        ]
    ];
    return Response::json($data, 404);
})->where('any', '.*');

I believe something like this should work.

takdw's avatar

@SHASHI_VERMA - @shashi_verma I looked at the question on StackOverflow. I'll just answer it here.

You have a POST /auth/signup endpoint in your api.php file. This endpoint is responsible to respond to POST requests only. But here you are sending a GET request to the endpoint. The thing is POST /auth/signup and GET /auth/signup are two different endpoints. The GET endpoint can not respond to POST requests and vice versa. This is clearly evident in the error Laravel is throwing when you try to hit /auth/signup as a POST request: MethodNotAllowedException.

Like I have stated in my previous post, you have to create an endpoint that catches any routes that couldn't be matched. Add the GET route I defined in my previous reply at the end of your api.php file and try it. You should get a 404 Error response in JSON format.

shashi_verma's avatar

@takdw I did the same as you can see there, it doesn't work with the APIs endpoints

Please or to participate in this conversation.