【问题标题】:POST request treated as GET in Heroku environment在 Heroku 环境中将 POST 请求视为 GET
【发布时间】:2014-07-04 11:32:30
【问题描述】:

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

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

无论如何(我尝试从 Java APP、Mozilla REST 客户端等)我尝试发送应该由 api 控制器中的创建操作处理的 POST HTTP 请求。在本地主机上 - 一切都按预期工作。在 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 缓存魔法?

【问题讨论】:

  • 你能用 curl 重现这个,并发布你的例子吗?
  • 感谢您激励我使用 curl,解决方案如下。
  • 我遇到了类似的问题:我发帖到myapp.heroku.com 而不是myapp.herokuapp.com——发帖到后者解决了这个问题。不过,从myapp.heroku.com 获取是可以的。

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


【解决方案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"} 

【讨论】:

    【解决方案2】:

    HTTP/1.1 301 永久移动

    301 重定向不是 Heroku 魔法。您的 DNS(或者可能是您的应用程序)可能会将所有 apex 请求 (mydomain.com) 转发到 www 子域。

    首选使用子域:

    【讨论】:

    • 凌晨 2 点;你刚刚拯救了我的夜晚!
    • 3 年后...仍在挽救生命
    【解决方案3】:

    我在不使用自定义域时遇到了类似的错误,只是因为一个容易被忽略的错误:我使用的是 heroku.com 而不是 herokuapp.com

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

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

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

    【讨论】:

    • 我也是。感谢您记录这一点!
    • 感谢您发布此信息;这可以解决我们的问题!快速澄清:my-app.heroku.com 在浏览器中为我们的应用程序解析得很好;为什么需要my-app.herokuapp.com?再次感谢。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2013-10-03
      • 2021-11-07
      • 2013-06-10
      • 1970-01-01
      • 2018-11-22
      相关资源
      最近更新 更多