4

Laravel API is not accepting JSON request. If I request as form-data it works but if I post JSON object in postman body then it does not receive the request data.

Route:

$router->group(['prefix' => 'imp'], function () use ($router) {
    $router->group(['prefix' => 'lead'], function () use ($router) {
        $router->post('/jardy', 'FeedController@jardy');
    });
});

Controller:

 public function jardy(Request $request)
    {

        $this->validate($request, [
            'api_key' => 'required',
        ]);
        $api_key = $request->input('api_key');
        return $api_key;
}

JSON Request:

Postman request using JSON

Form data Request:

Postman request using Form data

Why its not working in case of JSON, content-type is application/json, Accept:*/*???

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Imdad Ali
  • 727
  • 1
  • 8
  • 18
  • does `$request->validate([ 'api_key' => 'required' ])` work? – apokryfos Sep 02 '20 at 13:28
  • yes its working, when i use form-data it works. its like ```$this->validate($request, [ 'api_key' => 'required', ]);```. You have nissed ```$request```. – Imdad Ali Sep 02 '20 at 13:32
  • Can you try `->validate($request->all()` instead of `->validate($request` – Dilip Hirapara Sep 02 '20 at 13:33
  • ``` validate ``` method expects ```Request``` object. It shows error when i modify like you are suggesting. @Dilip Hirapara – Imdad Ali Sep 02 '20 at 13:36
  • Notice I am using the `validate` method on `$request` not on `$this` – apokryfos Sep 02 '20 at 13:47
  • @apokryfos ```validate``` method on ```$request``` shows ```error: Request::Validate does not exist```. – Imdad Ali Sep 03 '20 at 06:14
  • What laravel version are you using? – apokryfos Sep 03 '20 at 07:29
  • @apokryfos ```Laravel Framework Lumen (5.7.8) (Laravel Components 5.7.*)``` – Imdad Ali Sep 03 '20 at 10:35
  • That's odd, but you can also try `$this->validate($request->all(), [...])` sidenote, the `$request->validate` macro is registered via the [`FoundationServiceProvider`](https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php) so you can also check if that's registered. Not sure if Lumen registers that by default – apokryfos Sep 03 '20 at 12:20

4 Answers4

8

Comments are not permitted in JSON. There's a bug in the field Body -> raw -> json

gs-design
  • 96
  • 4
4

You have to add the header

Accept: application/json

Then laravel parses your RAW json as input vars and they can be accesed with ->input()

see: enter image description here

using / which is the postman default, will not work.. If you dont want to relay on the header, you could also do $request->json() but i guess, you just want to pass the header.

See the source that handles this: https://github.com/laravel/framework/blob/7.x/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php#L52

and https://github.com/laravel/framework/blob/7.x/src/Illuminate/Http/Concerns/InteractsWithContentTypes.php#L32

Danny Ebbers
  • 919
  • 5
  • 11
  • You are right, there is something I am doing wrong in Postman, Now I hit the API from an angular application, its working perfectly fine. – Imdad Ali Sep 03 '20 at 06:11
  • Of course Content-Type should also be application/json, as you suggested yourself in the original post. / You could inspect the request in your browser to see which headers your angular app is sending.. – Danny Ebbers Sep 03 '20 at 06:13
  • Did you disable the default *\/\* ?, I am pretty sure it only needs Accept and Content-Type headers, by default... It's also required to have laravel output json errors instead of html – Danny Ebbers Sep 03 '20 at 06:20
  • I added 2 links to the source, that is handling this... – Danny Ebbers Sep 03 '20 at 06:25
  • thank you @Danny, ```isJson()``` returns ```true```, but error is still the same when called from Postman. – Imdad Ali Sep 03 '20 at 06:38
  • @gs-design solution resolve the solution, it was the comments in the json objects, when i removed the comments, it worked. – Imdad Ali Oct 12 '20 at 09:07
1

In my case it was lack of Content-Length header.

Value should be a number of characters of request body.

Content-Type: application/json also should be present.

EugeneGpil
  • 91
  • 1
  • 3
0

I think its important to note that invalid json might be the problem here. In my case I was using postman variables and had to enclose them in double quotes

earlier I did {"client_id": {{client_id}}} and so when I changed to {"client_id" : "{{client_id}}"} it worked for me. Hope this helps

Carver
  • 59
  • 5