Google App Engine Go的PostForm方法为什么不发送任何url.Values?

huangapple go评论110阅读模式
英文:

Google App Engine Go PostForm not sending any url.Values?

问题

我在GAE的golang中有一个简单的函数:

  1. func Call(c appengine.Context, guid string, function string, parameters map[string]string) string {
  2. client := urlfetch.Client(c)
  3. values := url.Values{}
  4. c.Infof("%v", parameters)
  5. for k, v := range parameters {
  6. values.Set(k, v)
  7. }
  8. c.Infof("%v", values)
  9. resp, err := client.PostForm("https://blockchain.info/merchant/"+guid+"/"+function, values)
  10. var answer string
  11. if err != nil {
  12. c.Errorf("BlockchainAPI post error: %s", err)
  13. }
  14. c.Infof("%v", resp.Request.PostForm)
  15. [...]
  16. }

我得到了以下输出:

  1. 2013/10/14 23:17:51 INFO: map[main_password:password]
  2. 2013/10/14 23:17:51 INFO: map[main_password:
  3. 输入密码查看隐藏内容
  4. ]
  5. 2013/10/14 23:17:52 INFO: https://blockchain.info/merchant/guid/function
  6. 2013/10/14 23:17:52 INFO: map[]

看起来client.PostForm没有将values传递给请求,也没有在响应中获取它们。这可能是什么原因导致的错误?

英文:

I have a simple function in GAE golang:

  1. func Call(c appengine.Context, guid string, function string, parameters map[string]string) string {
  2. client:=urlfetch.Client(c)
  3. values := url.Values{}
  4. c.Infof("%v", parameters)
  5. for k, v := range parameters {
  6. values.Set(k, v)
  7. }
  8. c.Infof("%v", values)
  9. resp, err:=client.PostForm("https://blockchain.info/merchant/"+guid+"/"+function, values)
  10. var answer string
  11. if err != nil {
  12. c.Errorf("BlockchainAPI post error: %s", err)
  13. }
  14. c.Infof("%v", resp.Request.PostForm)
  15. [...]

I get these printouts:

  1. 2013/10/14 23:17:51 INFO: map[main_password:password]
  2. 2013/10/14 23:17:51 INFO: map[main_password:
  3. 输入密码查看隐藏内容
  4. ]
  5. 2013/10/14 23:17:52 INFO: https://blockchain.info/merchant/guid/function
  6. 2013/10/14 23:17:52 INFO: map[]

It looks as if client.PostForm does not pass values to the request and not get them back in response. What can be causing this error?

答案1

得分: 1

client.PostForm使用的是请求的Body而不是request.PostForm的值。

文档中有这样的说明:

  1. // PostForm包含来自POST或PUT请求的解析表单数据
  2. // body参数。
  3. // 只有在调用ParseForm之后,该字段才可用。
  4. // HTTP客户端忽略PostForm并使用Body。
  5. PostForm url.Values

所以你的代码需要做出改变,从:

  1. c.Infof("%v", resp.Request.PostForm)

改为类似下面的代码(我没有在字符串处理方面进行准确性测试):

  1. bd, _ := ioUtil.ReadAll(resp.Body)
  2. c.Infof("%v", string(bd[:len(bd)]))
英文:

<p>client.PostForm uses the body not the request.PostForm values.</p>
<p>It says so in the documentation :</p>

  1. // PostForm contains the parsed form data from POST or PUT
  2. // body parameters.
  3. // This field is only available after ParseForm is called.
  4. // The HTTP client ignores PostForm and uses Body instead.
  5. PostForm url.Values

<p>So your code needs to change from:</p>
c.Infof("%v", resp.Request.PostForm)

<p>To something like this (I haven't tested it for accuracy in the string handling):
bd, _ := ioUtil.ReadAll(resp.Body)
c.Infof("%v", string(bd[:len(bd)])
</p>

huangapple
  • 本文由 发表于 2013年10月15日 14:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/19374548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定