0

I'm making a Laravel 5.8 API (PHP 7.3.6) and the GET, POST and DELETE functions are all acting normally but I cannot get the PUT to behave as it should. When I use Postman and do a PUT request to "sites" it behaves like a GET request. The GET, POST and DELETE methods behave as I'd expect, i.e. go to the correct routes.

It was behaving like this on IIS but now I am using Docker with Apache and PHP 7.3.6 and it has the same behaviour.

It feels like it must be to do with Laravel, or maybe I am just using Postman wrong. Any ideas?

This is my api.php...

Route::get('sites', 'SelectSites@index');
Route::get('sites/{id}', 'SelectSites@show');
Route::put('sites', 'SelectSites@create');
Route::post('sites/{id}', 'SelectSites@update');
Route::delete('sites/{id}', 'SelectSites@delete');

This is the create method...

public function create(){
    $sites = "CREATE";
    return $sites;
}

This is the docker-compose.xml...

version: '3'
services:
app:
  build:
    context: .
    dockerfile: .docker/Dockerfile
  image: laravel-docker
  ports:
    - 8086:80

This is the VirtualHost file...

<VirtualHost *:80>
DocumentRoot /srv/app/public

<Directory "/srv/app/public">
    AllowOverride all
    Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined


    <Limit GET POST PUT DELETE>
      Order allow,deny Allow from all
    </Limit>

</VirtualHost>

UPDATE

This is working locally in IIS, I just had to restart my local machine. It also works perfectly in the PHP webserver php -S 0.0.0.0:8080. I have never used PUT before, is there something I need to do to configure Docker to use PUT?

sdexp
  • 756
  • 4
  • 18
  • 36
  • Try setting x-www-form-urlencoded for body in postman. – jtwes Aug 13 '19 at 14:55
  • Sorry, I meant to say that I'm using `x-www-form-urlencoded` and I even have key/values in the body incase it needs them. – sdexp Aug 13 '19 at 15:00
  • Are you prefixing the URL correctly for API routes, `/api/sites`? Are you absolutely certain you haven't defined this route twice? – matticustard Aug 13 '19 at 15:11
  • I actually cheated slightly and removed the API prefix from the Service for testing purposes. 100% sure it isn't defined twice though. – sdexp Aug 13 '19 at 15:13
  • Do you have the usual [form method spoofing](https://laravel.com/docs/5.8/routing#form-method-spoofing) field in your form? Try adding it, and doing a `POST`, as your real HTML form should do. – Don't Panic Aug 14 '19 at 09:09
  • I'm using the "Postman app", so not spoofing, using the actual HTTP request. – sdexp Aug 14 '19 at 09:11
  • Can you check `artisan route:list` ? – mniess Aug 14 '19 at 10:28
  • As pointed out in an answer, you have an error with your `put` and `post` routes. You mention that didn't solve it, so I'm guessing you have updated them somehow - pls update the question with what your routes currently look like. – Don't Panic Aug 14 '19 at 13:10

1 Answers1

0

As far as I can see you mixed up POST and PUT. A POST request is used for creating resources while PUT is used for updating.

mniess
  • 927
  • 11
  • 18