0
我似乎无法在我的应用程序中准备来自XML请求的body元素。无法读取请求xml body元素值
我试过BodyParameter
从Go-restulf软件包下面,但它似乎并没有工作,它只是返回零。
// BodyParameter parses the body of the request (once for typically a POST or a PUT) and returns the value of the given name or an error.
func (r *Request) BodyParameter(name string) (string, error) {
err := r.Request.ParseForm()
if err != nil {
return "", err
}
return r.Request.PostFormValue(name), nil
}
下面是我现在对我的文件:
type Account struct {
title, firstName, lastName, email, dob, countryCode, addrLevel, addUnitType, addrUnitNo, addrAllotment, addrBuildingNo, addrStreetName, addrStreetType, addrStreet1, addrStreet2, addrCity, countryState, addrPostcode, telephone, mobile string
userName, password string
currency float32
challenge1 string
response1 string
AdminUser, AdminPass, Version string
}
func (api *ApiResource) create(request *restful.Request, response *restful.Response) {
account := &Account{AdminUser: user, AdminPass: pass, Version: version}
err := request.ReadEntity(account)
if err != nil {
response.WriteErrorString(http.StatusInternalServerError, err.Error())
return
}
fmt.Println(request.BodyParameter("UserName"))
}
有为什么预期这不工作的原因?
你可以在操场上发布一个例子吗?当你说“它似乎不工作,它只是返回零”,我很好奇什么是不工作,返回值的字符串部分是否完全为零,或者它是一个空字符串和错误部分不是零。 – 2014-09-22 17:15:22
作为BodyParameter函数的一部分,@AustinMullins返回'r.Request.PostFormValue(name)'是以某种方式返回nil。我正在使用自定义包,所以我无法发布此示例对不起 – 2014-09-22 17:20:05
@PassionateDeveloper PostFormValue方法不能返回nil。也许你的意思是空的字符串。 PostFormValue方法返回空字符串,如果给定键没有值或者请求体不是表单。听起来好像请求主体可能是XML,而不是表单。 – 2014-09-22 17:32:34