Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post returns 405 method not allowed

I am having an issue with my Rails localhost Server, all Post calls started returning 405 method not allowed. However there are no problems on our staging and production servers. It is happening on all branches of code even ones that have not been updated. When debugging I see that it reaches the routes file but not the controller.

I have tried removing my gems and reinstalling, switching from WEBrick to Pama, creating a new clone of my git project.

Server

Started POST "/assets" for ::1 at 2015-07-14 12:14:27 -0400

Network Tab in Chrome

General

Remote Address:[::1]:3000
Request URL:http://localhost:3000/assets
Request Method:POST
Status Code:405 Method Not Allowed

Response

HTTP/1.1 405 Method Not Allowed
Cache-Control:no-cache
Content-Length:18
Content-Type:text/plain
X-Request-Id:9b0b2dd2-065b-4610-91c9-36494ea95353
X-Runtime:0.145368

Request

POST /assets HTTP/1.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8
    Cache-Control:no-cache
    Connection:keep-alive
    Content-Length:8376627
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryx8y8PBySdt7dxs4A
    Cookie:activeAccordionGroup=collapseTwo; _fusion360_hub_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWFlYmVlOGZjZmI3YzVlYjBjNjAyYzcyMzNhNzIyMzIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTllM2xhK1k4WG1hd2xYNnZCOEtHOEhPaHNTbWQvZGR2cGJ3bU9WUXIwRzg9BjsARg%3D%3D--4e108cb5f6eca3d986c0b3accec07bd2c27560b2; _mkto_trk=id:760-CWR-293&token:_mch-localhost-1435859445290-79614; _pk_id.845225.1fff=9847e7981c291a08.1435859445.1.1435859445.1435859445.; _allegorithmic-substance-marketplace_session=M3dMUCs4ZEtWSTFJTFVHV2VYN2pESFdHcGlHL2grVVVKSGxIWEZ3MlhXQkpRdHE2L0ZkMFpURmZDWGl6aTMxYSttMXFSQXN2M08zVVVXZTRHMDNKOHJOUzA1TmZoYnMwWURjb3c0Rkx6MTJYOW1Uem9aNGRObEMvc1NpSWo5VnQ4dUIzRnRtTFpnMlpOQVVZUU1SdWxiN1ZjN1lIMVd3Sk5jaXkyZkZLZ3duWTc4K2dnK0FSK29JVWdva2t0eUN1Q3hJbjFERHJVaGtndjVoWGxDRUlndz09LS1rZDdWcmtEWHlJWHRpZjc1MFNUSDF3PT0%3D--d34962721f449064dfdfd4629c0239ea1340aee4; __profilin=p%3Dt
    Host:localhost:3000
    Origin:http://localhost:3000
    Pragma:no-cache
    Referer:http://localhost:3000/assets/new
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
like image 436
Tyler Avatar asked Jul 14 '15 16:07

Tyler


People also ask

When should I return my 405?

405 Method Not Allowed should be returned by a server when a certain HTTP method is not supported at a resource. It's a bit different from 403 Forbidden . 403 suggest that the server might support the HTTP request, but the client doesn't have the right privileges to do the HTTP request.

What is Method not allowed in Postman?

However, in general, following HTTP standards, a 405 response status code means “Method Not Allowed”. So literally, a POST method is not allowed for that url endpoint on the server, in question. The server explicitly denies a POST method to that endpoint.


3 Answers

You have a route "assets", which is a reserved route in Rails and it is reserved for Rails Asset Pipeline.

If you must use the "assets" route then you need to give Asset Pipeline another mount point by adding the following line in your development.rb or production.rb configuration block:

config.assets.prefix = '/assetz'

And restart your server.

By this all your assets will be mounted on "/assetz" and you will be able to use POST requests on "/assets".

Happy coding!

Reference:

1) StackOverflow

like image 88
Jagjot Avatar answered Oct 23 '22 09:10

Jagjot


Could you post your routes file and also the exact rails version of your dev-environment and the production servers?

I assume this could happen when you post to a route that is only registered as a get request (depending on your rails version) or maybe routes that are defined twice, e.g.:

resources :photos, :only => [:index]
get :photos
like image 42
Alex Avatar answered Oct 23 '22 10:10

Alex


In the ruotes.rb file, include your routes in a namespace, so it should look like this:

Rails.application.routes.draw do
  namespace 'api' do
    Your routes here
  end
end

Then, add controllers corresponding to these routs in a folder with the same name of that namespace, so the app directory should look like this:

app
|  controllers
|  |  api
|  |  |  Your controller files here

Finally, the controllers should be inside a module with the same name of the namespace but with the first letter capital, so each controller should look like this:

  module Api
    Your controller code here
  end

Note: You can give each related set of routes/controllers different namespaces/modules. Also, you can use nested namespaces/modules.

like image 1
Ahmed Hussein Avatar answered Oct 23 '22 10:10

Ahmed Hussein