1

I initially thought there may be an error in my code, but I have done further tests where I am getting strange behaviour, and I was hoping to discover why. So I have a simple API route

Route::group(['prefix' => 'api/v1'], function () { Route::post('createProject', ['uses' => 'ProjectAPIController@createProject']); }); 

This calls a function that simply returns a success messsage

public function createProject(Request $request) { return response()->json(["Success", 200]); } 

The application I built to use this API was getting a bad request response, so I moved into Postman.

Now within Postman, if I do a Post request to this route, and I pass JSON via the body, I see the bad request still. However, if I change the JSON setting to text, I see the Success reponse. As such, I have discovered that the API for some reason is no longer liking JSON being sent to it.

Is there any reason this may be happening?

Thanks

1 Answer 1

1

You are not formatting the JSON correctly:

public function createProject(Request $request) { return response()->json(["Success", 200]); } 

The response should be in this format:

(data,statusCode)

So, the above response should be written like this:

return response()->json(["Success" => true],200); 

The data passed to json method's first parameter is associative array and then in the second parameter we pass the status code.

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

5 Comments

Thanks, is there any reason the API is not liking the JSON I am passing it though?
For that, I'll have to see the detailed response returned by the API call. Can you post it?
Hi, I have checked my logs and I see ModSecurity: JSON support was not enabled
Right, that explains it. So your request works fine now?
No, trying to work out what that error means, think it is server related

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.