【发布时间】:2014-09-16 12:48:41
【问题描述】:
以下调用以某种方式返回 base64 字符串而不是 xml 输出。我需要对此进行解码才能看到 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)
}
有没有办法防止这种情况发生并有正确的 xml 输出?
【问题讨论】:
-
http.POST不发送 base 64,除非明确要求。它返回什么,可能取决于它所联系的服务器。 -
请编辑您的问题,不要让它周围有任何绒毛。我假设您只对 POST 请求感兴趣,对吧?您应该给我们一个尝试您的代码的机会。
-
您在创建代理服务器吗?
-
@RickyA 是的,它被用作代理
-
@DewyBroto 我正在使用 Go-Restful 包。 github.com/emicklei/go-restful