xml - 无法读取请求 xml 正文元素值

标签 xml go

我似乎无法在我的应用程序中准备好来自 XML 请求的正文元素。

我尝试了下面 Go-restulf 包中的 BodyParameter,但它似乎不起作用,它只返回 nil。

// 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"))
}

这是否没有按预期工作?

最佳答案

AdminUser、AdminPass、Version 外,您的所有结构变量均未导出。

来自 http://golang.org/ref/spec#Exported_identifiers :

An identifier may be exported to permit access to it from another package. An identifier is exported if both:

  1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
  2. the identifier is declared in the package block or it is a field name or method name.

All other identifiers are not exported.

关于xml - 无法读取请求 xml 正文元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25979486/

相关文章:

web - 使用 Golang 构建服务器但在客户端出现问题

go - 如何在自定义类型上调用函数

c# - 从 XML 文档创建的 Json 反序列化到 POCO 不适用于数组

xml - 如何在 Excel 2011 for Mac 中解析 XML?

Java、Apache Commons 配置 XML 属性

Go 和 chromedp 的 Dockerfile

xml - 在 Web 浏览器中正确显示 XML

java - pretty-print 。忽略空格

go - 反射(reflect)接口(interface)列表

go - golang 应用程序的 Makefile 到 gradle 转换