如何从请求中读取数据,然后使用该结果进行POST请求,并处理其结果。

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

How to read from request then use that result to do POST request then process its results

问题

我正在尝试从请求中读取数据,然后使用该结果进行POST请求到另一个端点,然后处理其结果并以JSON格式返回结果。

到目前为止,我有以下代码:

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

api_resp, err := http.Post(Api.url, "text/plain", bytes.NewBuffer(usr))
if err != nil {
    response.WriteErrorString(http.StatusInternalServerError, err.Error())
    return
}
defer api_resp.Body.Close()
body, err := ioutil.ReadAll(api_resp.Body)
response.WriteHeader(http.StatusCreated)
err = xml.Unmarshal(body, usr)
if err != nil {
    fmt.Printf("error: %v", err)
    return
}

// result, err := json.Marshal(usr)
// response.Write(result)
response.WriteEntity(&usr)
fmt.Printf("Name: %q\n", usr.UserName)
}

我正在使用Go Restful包进行写入和读取。

当我编译文件时,我遇到以下错误:

src\login.go:59: cannot use usr (type *User) as type []byte in argument to bytes.NewBuffer

解决这个问题的最佳方法是什么,以便我可以正确地进行带有有效载荷的POST请求?

英文:

I'm trying to read from request then use that result to do POST request to another endpoint then process its results then return its results in JSON.

I have below code so far:

// POST 
func (u *UserResource) authenticate(request *restful.Request, response *restful.Response) {
	Api := Api{url: "http://api.com/api"}
	usr := new(User)
	err := request.ReadEntity(&usr)
	if err != nil {
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		return
	}
	
	api_resp, err := http.Post(Api.url, "text/plain", bytes.NewBuffer(usr))
	if err != nil {
		response.WriteErrorString(http.StatusInternalServerError, err.Error())
		return
	}
	defer api_resp.Body.Close()
	body, err := ioutil.ReadAll(api_resp.Body)
	response.WriteHeader(http.StatusCreated)
	err = xml.Unmarshal(body, usr)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
//	result, err := json.Marshal(usr)
//	response.Write(result)
	response.WriteEntity(&usr)
	fmt.Printf("Name: %q\n", usr.UserName)
}

I'm using Go Restful package for Writes and Reads.

I'm getting this error when I compile the file:

src\login.go:59: cannot use usr (type *User) as type []byte in argument to bytes.NewBuffer

What would be the best way to solve this issue so I can do a POST with payload correctly?

答案1

得分: 1

你需要将你的数据结构编组为字节切片。类似这样:

usrXmlBytes, err := xml.Marshal(usr)
if err != nil {
    response.WriteErrorString(http.StatusInternalServerError, err.Error())
    return
}
api_resp, err := http.Post(Api.url, "text/plain", bytes.NewReader(usrXmlBytes))
英文:

You need to marshal your data structure to slice of bytes. Something like this:

usrXmlBytes, err := xml.Marshal(usr)
if err != nil {
	response.WriteErrorString(http.StatusInternalServerError, err.Error())
	return
}
api_resp, err := http.Post(Api.url, "text/plain", bytes.NewReader(usrXmlBytes))

答案2

得分: 0

http.Post函数的第三个参数需要一个io.Reader类型的参数。你可以在你的User类型上实现io.Reader接口,或者更简单地将你的数据序列化,并使用bytes包来实现io.Reader

b, err := json.Marshal(usr)
if err != nil {
    response.WriteErrorString(http.StatusInternalServerError, err.Error())
    return
}
api_resp, err := http.Post(Api.url, "text/plain", bytes.NewReader(b))
英文:

http.Post takes an io.Reader as the third argument. You could implement io.Reader on your User type or more simply serialize your data and use the bytes pkg to to implement io.Reader

b, err := json.Marshal(usr)
if err != nil {
    response.WriteErrorString(http.StatusInternalServerError, err.Error())
    return
}
api_resp, err := http.Post(Api.url, "text/plain", bytes.NewReader(b))

huangapple
  • 本文由 发表于 2014年9月17日 21:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/25892330.html
匿名

发表评论

匿名网友

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

确定