Golang的net/http包中的Post调用返回base64编码。

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

Golang net/http package Post call returns base64

问题

一些原因导致下面的调用返回了Base64字符串而不是XML输出。我需要解码它以查看XML。

  1. // POST
  2. func (u *UserResource) authenticateUser(request *restful.Request, response *restful.Response) {
  3. Api := new(Api)
  4. Api.url = "http://api.com"
  5. usr := new(User)
  6. err := request.ReadEntity(usr)
  7. if err != nil {
  8. response.AddHeader("Content-Type", "application/json")
  9. response.WriteErrorString(http.StatusInternalServerError, err.Error())
  10. return
  11. }
  12. buf := []byte("<api version=\"6.0\"><request>test</request></api>")
  13. r, err := http.Post(Api.url, "text/plain", bytes.NewBuffer(buf))
  14. if err != nil {
  15. response.AddHeader("Content-Type", "plain/text")
  16. response.WriteErrorString(http.StatusInternalServerError, err.Error())
  17. return
  18. }
  19. defer r.Body.Close()
  20. body, err := ioutil.ReadAll(r.Body)
  21. response.WriteHeader(http.StatusCreated)
  22. response.WriteEntity(body)
  23. }

有没有办法防止这种情况发生,并获得正确的XML输出?

英文:

Somehow below call returns base64 string instead of xml output. I need to decode this to see xml.

  1. // POST
  2. func (u *UserResource) authenticateUser(request *restful.Request, response *restful.Response) {
  3. Api := new(Api)
  4. Api.url = &quot;http://api.com&quot;
  5. usr := new(User)
  6. err := request.ReadEntity(usr)
  7. if err != nil {
  8. response.AddHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)
  9. response.WriteErrorString(http.StatusInternalServerError, err.Error())
  10. return
  11. }
  12. buf := []byte(&quot;&lt;api version=\&quot;6.0\&quot;&gt;&lt;request&gt;test&lt;/request&gt;&lt;/api&gt;&quot;)
  13. r, err := http.Post(Api.url, &quot;text/plain&quot;, bytes.NewBuffer(buf))
  14. if err != nil {
  15. response.AddHeader(&quot;Content-Type&quot;, &quot;plain/text&quot;)
  16. response.WriteErrorString(http.StatusInternalServerError, err.Error())
  17. return
  18. }
  19. defer r.Body.Close()
  20. body, err := ioutil.ReadAll(r.Body)
  21. response.WriteHeader(http.StatusCreated)
  22. response.WriteEntity(body)
  23. }

Is there a way to prevent this from happening and have correct xml output?

答案1

得分: 3

代码使用Go-Restful的WriteEntity方法将包含XML的[]byte写入响应体中。WriteEntity方法使用标准编码包将值编组为XML或JSON。这些包将[]byte值编组为base64字符串。

将上面的最后一行更改为

  1. response.Write(body)

将以不进行JSON或XML编码的方式将远程服务器的响应写入客户端。

英文:

The code uses the Go-Restful WriteEntity method to write a []byte containing XML to the response body. The WriteEntity method marshals the value to XML or JSON using the standard encoding packages. These packages marshal []byte values as base64 strings.

Changing the last line above to

  1. response.Write(body)

will write the remote server's response to the client without JSON or XML encoding.

huangapple
  • 本文由 发表于 2014年9月16日 20:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/25869217.html
匿名

发表评论

匿名网友

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

确定