Cant reach my put and post routes [SOLVED]

I have my server setup with the following routes.

GET

$app->get('/books/{id}', function ($request, $response, $args) {
         var_dump("GET Request");
         die();
     });

PUT

$app->put('/books/{id}', function ($request, $response, $args) {
         var_dump("PUT Request");
         die();
});

When i try to update a book i use postman to preform a HTTP PUT request and fill in the url.
Give the request a json body with the changes that need to be applied to the book and send the request.
The response i get from the server is “GET Request” where i specifficly set the request type to be PUT.

Has anyone encountered this problem before?

I do not think it will work with the same route URL
You can make it as multiple methods in one rout
like this

$app->map(['GET', 'POST'], '/books/{id}', function ($request, $response, $args) {
//$request->isGet()
      //Get method work 
   //$request->isPost()
     // Put method Work
});

@Ramyhakam, actually it will work with the same route url. However, as far as I know, html forms can’t perform PUT or DELETE requests, only GET and POST but XHR requests can specify PUT and DELETE.

$app->get('/book/{id}',function($request,$response,$args) {
  // get the record by id
});

$app->post('/book',function($request,$response,$args) {
  // create a new record
});

$app->put('/book/{id}',function($request,$response,$args) {
  // update a record by id (XHR Only??)
});

$app->delete('/book/{id}',function($request,$response,$args) {
  // delete a record by id (XHR Only??)
});

I have tested the routes when i enrolled the REST api to an online server and they were working just fine.

After a month this issue started to come up. now both from Android and Postman i can’t manage to preform a properly working put request. So that kinda is the strange part here.

I have seen now that i use regular web requests instead of xhr requests in android. so i go try that. but that still does not explain the disfunction in postman right?

@devc0n I’ve tested those routes on my machine in Postman and the results I get back are legit. Could it be a CORS issue where a PUT request is not allowed on your server?

1 Like

Thanks for the mention of CORS i was not aware of the existence of this. i asked my hosting provider and they told me they do not support CORS. so i guess i will just use get and post to do all of my routes. Thanx for the help guys!