duansao6776 2014-09-16 12:48
浏览 121
已采纳

Golang net / http包Post调用返回base64

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

// POST 
func (u *UserResource) authenticateUser(request *restful.Request, response *restful.Response) {
    Api := new(Api)
    Api.url = "http://api.com"
    usr := new(User)
    err := request.ReadEntity(usr)
    if err != nil {
        response.AddHeader("Content-Type", "application/json")
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return
    }

    buf := []byte("<api version=\"6.0\"><request>test</request></api>")

    r, err := http.Post(Api.url, "text/plain", bytes.NewBuffer(buf))
    if err != nil {
        response.AddHeader("Content-Type", "plain/text")
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return
    }
    defer r.Body.Close()
    body, err := ioutil.ReadAll(r.Body)
    response.WriteHeader(http.StatusCreated)
    response.WriteEntity(body)
}

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

  • 写回答

1条回答 默认 最新

  • douci2015 2014-09-16 14:02
    关注

    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

    response.Write(body)
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计