Http 如何在go server中提取post参数

Http 如何在go server中提取post参数,http,post,get,go,Http,Post,Get,Go,下面是一个用go编写的服务器 package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) fmt.Fprintf(w,"%s",r.Method) } func main() { http.HandleFunc(

下面是一个用go编写的服务器

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
    fmt.Fprintf(w,"%s",r.Method)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

如何提取发送到
localhost:8080/something
URL的
POST
数据?

引用

像这样:

func handler(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()                     // Parses the request body
    x := r.Form.Get("parameter_name") // x will be "" if parameter is not set
    fmt.Println(x)
}

要从post请求中提取值,首先必须调用
r.ParseForm()
。[This][1]从URL解析原始查询并更新r.Form

对于POST或PUT请求,它还将请求主体解析为表单 并将结果同时放入r.PostForm和r.Form中。投递体 参数优先于r.Form中的URL查询字符串值

现在,您的
r.From
是客户提供的所有值的映射。要提取特定的值,可以使用
r.FormValue(“”
r.Form.Get(“”


您还可以使用
r.PostFormValue

处理POST补丁PUT请求:

err := r.ParseForm()
if err != nil {
    // in case of any error
    return
}

// Use the r.Form.Get() method to retrieve the relevant data fields
// from the r.Form map.
value := r.Form.Get("parameter_name") // attention! r.Form, not r.PostForm 
首先,我们调用
r.ParseForm()
,它将POST请求主体中的任何数据添加到
r.PostForm
映射中

err := r.ParseForm()
if err != nil {
    // in case of any error
    return
}

// Use the r.PostForm.Get() method to retrieve the relevant data fields
// from the r.PostForm map.
value := r.PostForm.Get("parameter_name")

对于POSTGETPUT等(对于所有请求):

表单方法

相反,r.Form映射是为所有请求填充的 (不考虑其HTTP方法),并包含来自 任何请求正文和任何查询字符串参数。那么,如果我们的表格是 提交到/snippet/create?foo=bar,我们还可以得到 通过调用r.Form.Get(“foo”)调用foo参数。请注意 对于冲突,请求主体值将优先于 查询字符串参数

FormValue
PostFormValue
方法

net/http包还提供了方法r.FormValue()和 r、 PostFormValue()。这些基本上是调用 r、 ,然后从中获取适当的字段值 r、 分别为Form或r.PostForm。我建议避免这些捷径 因为它们会默默地忽略r.ParseForm()返回的任何错误。 这并不理想-这意味着我们的应用程序可能会遇到 错误和失败的用户,但没有反馈机制让 他们知道

所有样品都来自于关于围棋的最佳书籍。这本书可以回答你所有的问题

对于正常请求:

r.ParseForm()
value := r.FormValue("value")
对于多部分请求:

r.ParseForm()
r.ParseMultipartForm(32 << 20)
file, _, _ := r.FormFile("file")
r.ParseForm()
r、 ParseMultipartForm(32
您可以在aapi中运行代码

将其添加到访问url:/MyPathParameter?param1=myqueryparam

我想暂时离开上面的链接,因为它为您提供了一个运行代码的好地方,并且我相信能够看到它的实际运行是很有帮助的,但是让我来解释一些您可能需要post参数的典型情况

开发人员将post数据拉入后端服务器有几种典型的方式,通常在从请求中拉入文件或大量数据时会使用多部分表单数据,因此我不认为这与此有什么关系,至少在问题的上下文中是这样。他正在寻找post参数,通常意味着表单post数据。通常y表单post参数以web形式发送到后端服务器

  • 当用户从html向golang提交登录表单或注册数据时,在本例中,来自客户端的内容类型头通常是application/x-www-form-urlencoded,我相信这就是问题所在,这些将是表单post参数,使用r.FormValue(“param1”)提取

  • 在从post主体获取json的情况下,您可以获取整个post主体并将原始json解组到一个结构中,或者在从请求主体提取数据后使用库解析数据,即Content Type header application/json


  • Content Type header主要负责如何解析来自客户端的数据,我已经给出了两种不同内容类型的示例,但还有更多。

    请您用一个更具体的示例。我是新手,不知道如何使用它。请注意,这似乎只适用于URLCoded,而不适用于multipart form data.PS:这方面的文档目前很差,也没有很好的示例。只是说……是的,这些类型的PS是不需要的。OP不是问这个问题,他问的是如何提取POST数据。将所有这些信息编辑到您的答案中,这将是对OP的更好反应。一个简单的代码片段也是不够的k答案有点不受欢迎,因为链接可能会断开/丢失。这是一个很好的建议,我编辑了我的答案,进一步的建议会很有帮助。我是新加入此表单的人,希望学习。@Psychotechnopath你们可能想更改这里关于如何回答问题的陈述,因为这里说链接是鼓励的老年人。老实说,我认为你有时需要链接来帮助描述。如果我在这里不正确,请告诉我。“但请在链接周围添加上下文,这样你的其他用户就会知道它是什么以及为什么存在。始终引用重要链接的最相关部分,以防无法访问目标站点或永久脱机。”.我只是说只有链接的答案不好。
    r.ParseForm()
    r.ParseMultipartForm(32 << 20)
    file, _, _ := r.FormFile("file")
    
    package main
    
    import (
      "fmt"
      "log"
      "net/http"
      "strings"
    )
    
    
    func main() {
      // the forward slash at the end is important for path parameters:
      http.HandleFunc("/testendpoint/", testendpoint)
      err := http.ListenAndServe(":8888", nil)
      if err != nil {
        log.Println("ListenAndServe: ", err)
      }
    }
    
    func testendpoint(w http.ResponseWriter, r *http.Request) {
      // If you want a good line of code to get both query or form parameters
      // you can do the following:
      param1 := r.FormValue("param1")
      fmt.Fprintf( w, "Parameter1:  %s ", param1)
    
      //to get a path parameter using the standard library simply
      param2 := strings.Split(r.URL.Path, "/")
    
      // make sure you handle the lack of path parameters
      if len(param2) > 4 {
        fmt.Fprintf( w, " Parameter2:  %s", param2[5])
      }
    }