使Go的http.Response详细显示所有参数

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

Make Go http.Response verbose all parameters

问题

我遇到了一个问题,无法将从Android应用程序发送的参数传递到Go应用程序中。我调用了r.FormValue(key),但它返回了null。我想找到一种方法,在Android应用程序将POST数据发送到Go应用程序后,检查Go端可用的参数有哪些。有没有一种方法可以获取所有参数而不使用键来实现这一点?

英文:

I am having a problem getting a parameter sent from android app into go application. I called r.FormValue(key) but it returned null. I want to find the way to check what are parameters available on Go side after the android app sent the post data to it. Is there any way to do this, getting all parameters without using keys?

答案1

得分: 6

在Go语言中,Request结构体具有一个Form字段,该字段在调用ParseForm()后会填充请求参数。

> Form字段包含解析后的表单数据,包括URL字段的查询参数和POST或PUT表单数据。只有在调用ParseForm之后,该字段才可用。HTTP客户端会忽略Form字段,而使用Body字段。

你可以在接收到请求后尝试添加以下代码:

func(w http.ResponseWriter, request *http.Request) {
    request.ParseForm()
    log.Printf("%v",request.Form)

    //....
}
英文:

The Request structure in go has a Form field which is populated with request parameters after ParseForm() is called.

> Form contains the parsed form data, including both the URL field's
> query parameters and the POST or PUT form data.This field is only
> available after ParseForm is called. The HTTP client ignores Form and
> uses Body instead.

You could try adding the following code after receiving a request:

func(w http.ResponseWriter, request *http.Request) {
    request.ParseForm()
    log.Printf("%v",request.Form)

    //....
}

答案2

得分: 2

如果这是用于调试,你可以使用DumpRequest

func(w http.ResponseWriter, r *http.Request) {
    dump, err := httputil.DumpRequest(r, true)
    if err != nil {
        http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
        return
    }
    log.Printf("%s", dump)
}
英文:

If this is for debugging, you can use DumpRequest:

func(w http.ResponseWriter, r *http.Request) {
	dump, err := httputil.DumpRequest(r, true)
	if err != nil {
		http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
		return
	}
    log.Printf("%s", dump)
}

huangapple
  • 本文由 发表于 2016年4月25日 19:54:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/36839830.html
匿名

发表评论

匿名网友

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

确定