http - 如何在go服务器中提取post参数

标签 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("/", 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)
}

关于http - 如何在go服务器中提取post参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512009/

相关文章:

authentication - 是否可以从浏览器URL发出发布请求

javascript - 无法访问ajax代码中的变量

perl - USPS HTTP 邮寄请求

c - 使用 Wininet 发送多个 Http 请求

c - 制作一个简单的HTTP网络服务器,使用errno回复 "403 Forbidden",然后重定向到403页面?

java - 无法让 HTTP Post 工作

javascript - 为什么请求无法访问该网站的安全页面?

javascript - Express - 如何从 POST 表单获取数据

spring-boot - CORS 与 Spring Boot - 将 GET 请求限制到某些域

java - Hashtable get 方法是否返回多个值?