使用golang http包获取发布数据 [英] Get post data using golang http package

查看:122
本文介绍了使用golang http包获取发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是golang的新手,熟悉http包。



http:// localhost:8084 / dbTest 是我的URI。我使用表单数据传递键: hub_id 值: 1 。我尝试了下面的方法,

  req.ParseForm()
fmt.Println(hub_id,req.Form [ hub_id])
req.Form.Get(hub_id)

但是没有的方法工作。



以下是我的代码:

 包主要

导入(
fmt
净/ http
记录


func dbtest (w http.ResponseWriter,req * http.Request){
req.ParseForm()
fmt.Println(hub_id,req.Form [hub_id])
req.Form .get(hub_id)
fmt.Println(req.PostFormValue(hub_id))//响应为空
}

func main(){

http.HandleFunc(/ dbTest,dbtest)

log.Fatal(http.ListenAndServe(:8084,nil))
}

当我打印请求时,我得到以下内容:

 & {POST / dbTest HTTP / 1.1 1 1 map [Origin:[chrome-extension:// fhbjgbiflinjbdggehcddcbncdddomop] Connection:[keep-alive] Content-Type:[multipart / form-data;边界= ---- WebKitFormBoundarydFOTVjOJMeqOHnS3]内容长度:[138] Accept-Language:[en-US,en; q = 0.8] Cache-Control:[no-cache] Accept-Encoding:[gzip,deflate] Accept: [* / *] User-Agent:[Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 52.0.2743.82 Safari / 537.36] Postman-Token:[ac7ae3a9-60f6-2146-3f1c- 209de7622774]] 0xc210012e70 138 [] false localhost:8084 map [] map []< nil> map [] 127.0.0.1:34152 / dbTest< nil>} 

解决方案:我找到了解决方案。因为内容类型是 mulipart / form-data 我正确的解析表单的方式是使用 req.ParseMultipartForm http方法。

解决方案

您正在使用不正确的 Content-Type 。当使用 Content-Type:application / x-www-form-urlencoded r.ParseForm()将解析数据正确。之后检查 r.Form


I am new to golang, getting familiar with http package. I am having trouble in getting the post data I am sending using postman.

http://localhost:8084/dbTest is my URI. I am passing key: hub_id value: 1 using form-data. I tried following approaches,

req.ParseForm()
fmt.Println("hub_id", req.Form["hub_id"])
req.Form.Get("hub_id")

But none of the approach work. I am getting empty response.

Following is my code:

package main

import (
    "fmt"
    "net/http"
    "log"
) 

func dbtest(w http.ResponseWriter, req *http.Request) {
  req.ParseForm()
  fmt.Println("hub_id", req.Form["hub_id"])
  req.Form.Get("hub_id")
  fmt.Println(req.PostFormValue("hub_id")) //response is empty
}

func main() {

    http.HandleFunc("/dbTest", dbtest)

  log.Fatal(http.ListenAndServe(":8084", nil))
}

When I print req i get the following:

&{POST /dbTest HTTP/1.1 1 1 map[Origin:[chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop] Connection:[keep-alive] Content-Type:[multipart/form-data; boundary=----WebKitFormBoundarydFOTVjOJMeqOHnS3] Content-Length:[138] Accept-Language:[en-US,en;q=0.8] Cache-Control:[no-cache] Accept-Encoding:[gzip, deflate] Accept:[*/*] User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36] Postman-Token:[ac7ae3a9-60f6-2146-3f1c-209de7622774]] 0xc210012e70 138 [] false localhost:8084 map[] map[] <nil> map[] 127.0.0.1:34152 /dbTest <nil>}

Solution: I found the solution. Since, content-type is mulipart/form-data I correct way to parse form is to use req.ParseMultipartForm http method.

解决方案

You are using incorrect Content-Type. When using Content-Type:application/x-www-form-urlencoded, r.ParseForm() will parse data correctly. Check r.Form afterwards.

这篇关于使用golang http包获取发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆