在Heroku环境中将POST请求视为GET

时间:2014-05-15 20:32:23

标签: ruby-on-rails http post heroku get

我有奇怪的情况。我有一个RoR应用程序,它提供了我从Java应用程序连接的REST API。

我正在本地开发RoR,并在Heroku环境中部署它。

无论如何(我尝试从Java APP,Mozilla REST客户端等)我尝试发送应该由api控制器中的create action处理的POST HTTP请求。在localhost上 - 一切都按预期工作。在Heroku生产环境中 - POST请求被视为正常的GET。

以下是此资源的路线:

        api_v1_items GET    /api/v1/items(.:format)                            api/v1/items#index {:format=>:json}
                     POST   /api/v1/items(.:format)                            api/v1/items#create {:format=>:json}
         api_v1_item GET    /api/v1/items/:id(.:format)                        api/v1/items#show {:format=>:json}
                     PATCH  /api/v1/items/:id(.:format)                        api/v1/items#update {:format=>:json}
                     PUT    /api/v1/items/:id(.:format)                        api/v1/items#update {:format=>:json}
                     DELETE /api/v1/items/:id(.:format)                        api/v1/items#destroy {:format=>:json}

所以我正在尝试向/api/v1/items传递所有必要参数的POST请求。

在localhost中,响应是正确的:

Started POST "/api/v1/items?token=l4XOHrhDApPqTp1u4TxBjQ" for 127.0.0.1 at 2014-05-15 22:11:49 +0200
Processing by Api::V1::ItemsController#create as JSON
Parameters: {"height"=>10.0, "item_name"=>"Super item", "width"=>20.0, etc...

然而,同样的请求在Heroku被解雇为GET:

2014-05-15T20:27:58.137541+00:00 app[web.1]: Started GET "/api/v1/items?token=iEdDkDLiDUlWi0mDbr6XYw" for 89.74.57.51 at 2014-05-15 20:27:58 +0000
2014-05-15T20:27:58.223620+00:00 app[web.1]: Processing by Api::V1::ItemsController#index as JSON

有什么想法吗?当然两个回购都是同步的。检查几次。

这真的很奇怪......也许是某种Heroku缓存魔法?

4 个答案:

答案 0 :(得分:7)

  

HTTP / 1.1 301永久移动

301重定向不是 Heroku magic 。您的DNS(或可能是您的应用)可能会将所有顶级请求(mydomain.com)转发到www子域。

首选使用子域:

答案 1 :(得分:4)

由于一个容易被忽视的错误,我在使用自定义域时遇到了类似的错误:我使用的是heroku.com而不是heroku app .com

错误的:     http://my-app.heroku.com

右:     http://my-app.herokuapp.com

我怀疑它与Catsby's answer中提到的问题非常相似。

答案 2 :(得分:1)

好吧,我试过CURL,看起来错误是愚蠢的。

我在http://mydomain.com发帖,在那里被作为GET路由。 当我在http://www.mydomain.com开火时 - 它有效。

Heroku魔法。

以下是curl和结果供您参考。也许有人能够解释为什么它会像这样......

在mydomain.com发布

curl -v -H "Accept: application/json" -H "Cont"width":20.0,"item_desc":"The super item","std_pack":40,"sku":"A1004","depth":20.0}}'  http://mydomain.com/api/v1/items?token=dSWeyKjjtZu0ZSs6b2J-yw
* Adding handle: conn: 0x7fe70b803000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fe70b803000) send_pipe: 1, recv_pipe: 0
* About to connect() to mydomain.com port 80 (#0)
*   Trying 78.46.51.229...
* Connected to mydomain.com (78.46.51.229) port 80 (#0)
> POST /api/v1/items?token=dSWeyKjjtZu0ZSs6b2J-yw HTTP/1.1
> User-Agent: curl/7.30.0
> Host: mydomain.com
> Accept: application/json
> Content-type: application/json
> Content-Length: 174
> 
* upload completely sent off: 174 out of 174 bytes
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 15 May 2014 21:20:58 GMT
* Server Apache is not blacklisted
< Server: Apache
< Location: http://www.mydomain.com/api/v1/items?token=dSWeyKjjtZu0ZSs6b2J-yw
< Content-Length: 273
< Content-Type: text/html; charset=iso-8859-1
< 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.mydomain.com/api/v1/items?token=dSWeyKjjtZu0ZSs6b2J-yw">here</a>.</p>
</body></html>
* Connection #0 to host mydomain.com left intact

在www.mydomain.com发布

Maciejs-MacBook-Pro:merchbag maciejsimm$ curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"item":{"height":10.0,"item_name":"Super duper item","width":20.0,"item_desc":"The super","std_pack":40,"sku":"A1005","depth":20.0}}'  http://www.mydomain.com/api/v1/items?token=dSWeyKjjtZu0ZSs6b2J-yw
* Adding handle: conn: 0x7fc191003000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fc191003000) send_pipe: 1, recv_pipe: 0
* About to connect() to www.mydomain.com port 80 (#0)
*   Trying 50.17.185.176...
* Connected to www.mydomain.com (50.17.185.176) port 80 (#0)
> POST /api/v1/items?token=dSWeyKjjtZu0ZSs6b2J-yw HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www.mydomain.com
> Accept: application/json
> Content-type: application/json
> Content-Length: 133
> 
* upload completely sent off: 133 out of 133 bytes
< HTTP/1.1 201 Created 
< Cache-Control: max-age=0, private, must-revalidate
< Content-Type: application/json; charset=utf-8
< Date: Thu, 15 May 2014 21:24:17 GMT
< Etag: "41231ae0f50a604cd7316a014d19b3f2"
* Server WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08) is not blacklisted
< Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08)
< Set-Cookie: request_method=POST; path=/
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-Request-Id: ba05dd74-bf52-47d5-b8a9-d0516aff5804
< X-Runtime: 0.020289
< X-Ua-Compatible: chrome=1
< X-Xss-Protection: 1; mode=block
< Content-Length: 234
< Connection: keep-alive
< 
* Connection #0 to host www.mydomain.com left intact
{"id":15,"partner_id":1,"sku":"A1005","item_name":"Super duper item","item_desc":"The super","std_pack":40,"height":10,"width":20,"depth":20,"image":null,"created_at":"2014-05-15T21:24:17.753Z","updated_at":"2014-05-15T21:24:17.761Z"} 

答案 3 :(得分:-1)

使用HTTP而不是HTTPS向heroku发送POST请求时遇到了同样的问题。每次heroku都将我的POST请求作为GET请求路由。一旦我更新了url以使用HTTPS,我的POST请求被heroku路由为POST而不是GET,解决了问题。之前帖子中提到的重定向问题可能是我遇到的问题的根本原因。