编辑:解决了!服务器正在从/whales 重定向到/whales/,这将请求转换为 GET。我的 curl 命令有尾部斜杠,但我的表单和 Postman 请求没有。
我的基本服务器始终将“GET”作为 r.Method,即使对于来自 Postman 和 html 表单的发布请求也是如此。 r.Form 始终是一个空映射。
我的代码:
func whaleHandler(w http.ResponseWriter, r *http.Request) {
fmt.Print(r.Method)
fmt.Print(r.Form)
}
func main() {
http.HandleFunc("/whales/", whaleHandler)
log.Fatal(http.ListenAndServe(":9002", nil))
}
然后打印:
GETmap[]
我在这里做错了什么?在此先感谢大家!
编辑:使用 curl 一切正常,但 Postman 和常规表单仍被视为 GET 请求。
curl -d "Name=Barry"-X POST http://localhost:9002/whales/
结果:
POSTmap[],
和r.FormValue("Name")吐出Barry
示例表格:
<form action="/whales" method="POST">
<div><input type="text" name="Name" placeholder="Name"></div>
<div><input type="text" name="Length" placeholder="Length"></div>
<div><input type="text" name="Type" placeholder="Type"></div>
<div><input type="submit" value="Save"></div>
</form>
fmt.Print(r) 的完整输出,为了便于阅读,进行了一些格式化:
&{GET
/whales/
HTTP/1.1
1
1
map[
Accept:[
text/html,
application/xhtml+xml,
application/xml;q=0.9,*\/*;q=0.8
]
Accept-Language:[en-US,en;q=0.5]
Accept-Encoding:[gzip, deflate]
Cookie:[io=08-aNjAMs8v6ntatAAAA]
Connection:[keep-alive]
Upgrade-Insecure-Requests:[1]
User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:58.0) Gecko/20100101 Firefox/58.0] Referer:[http://localhost:9002/whales/create]
]
{}
<nil>
0
[]
false
localhost:9002
map[]
map[]
<nil>
map[]
[::1]:61037
/whales/
<nil>
<nil>
<nil>
0xc420162400}
编辑: 示例 Postman 请求,结果为 r.Method => "GET" Posting with Postman
最佳答案
您在“/whales/”下注册了您的处理程序,但您的表单操作是“/whales”(没有尾部斜线)。在此配置中,Go 会将请求从/whales 重定向到/whales/——大多数客户端选择通过 GET 请求进行跟进。
要么注册“/whales”的处理程序,要么将表单操作更改为“/whales/”,具体取决于您希望处理的其他 URL。例如,如果您需要处理/whales/willy,请保持处理程序不变并更改表单操作。
关于http - 服务器认为所有请求都有 r.Method "GET",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49300520/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru