首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Heroku环境中将POST请求作为GET处理

在Heroku环境中将POST请求作为GET处理
EN

Stack Overflow用户
提问于 2014-05-15 20:32:23
回答 4查看 4.2K关注 0票数 8

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

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

无论如何(我尝试了Java应用程序、Mozilla REST客户端等)我试图发送POST HTTP请求,这些请求应该由api控制器中的create处理。在本地主机上-一切都如预期的那样工作。在Heroku生产env -- POST请求被视为正常的GET。

以下是我获得这个资源的路线:

代码语言:javascript
运行
复制
        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请求,传递所有必要的参数。

在本地主机中,响应是正确的:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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藏魔法?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-05-16 20:02:37

HTTP/1.1 301永久移动

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

优先使用子域:

票数 7
EN

Stack Overflow用户

发布于 2015-07-03 15:49:10

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

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

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

我怀疑这与卡茨比的回答中提到的问题非常相似。

票数 5
EN

Stack Overflow用户

发布于 2014-05-15 21:26:32

嗯,我试过卷发,看起来错误是愚蠢的。

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

Heroku魔法。

下面是卷曲和结果,供您参考。也许有人能解释为什么会这样..。

在mydomain.com的职位

代码语言:javascript
运行
复制
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的职位

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23688067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档