亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何訪問 POST 請求中的嵌入鍵值

如何訪問 POST 請求中的嵌入鍵值

Go
桃花長相依 2022-06-27 16:36:47
我正在用 Golang 制作一個輪盤賭 REST API:package mainimport (    "fmt"    "io/ioutil"    "log"    "net/http"    "github.com/gorilla/mux")func handleRequests() {    // creates a new instance of a mux router    myRouter := mux.NewRouter().StrictSlash(true)        myRouter.HandleFunc("/spin/", handler).Methods("POST")    log.Fatal(http.ListenAndServe(":10000", myRouter))}func handler(w http.ResponseWriter, r *http.Request) {    reqBody, _ := ioutil.ReadAll(r.Body)    s := string(reqBody)    fmt.Println(s)}func main() {    fmt.Println("Rest API v2.0 - Mux Routers")    handleRequests()}main.go我正在使用 Python 腳本測試 POST 方法:import requestsurl = 'http://localhost:10000/spin/'myobj = {'bets':[                {                    'amount' : 10,                    'position' : [0,1,2]                },                {                    'amount' : 20,                    'position' : [10]                }            ]}x = requests.post(url, data = myobj)print(x.text)test.py當我運行測試腳本時。服務器收到我的 POST 請求。請求正文是: bets=amount&bets=position&bets=amount&bets=position問題是'amount'和'position'鍵的值不存在。我的問題是 - 我如何制作/處理 POST 請求,以便能夠訪問嵌入鍵的值'amount'和'position'Go 服務器上的處理程序函數中的值,以便我可以將此信息放入結構的實例中。
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

如果您打印出請求的正文/標頭,則問題出在 python 方面:


print requests.Request('POST', url, data=myobj).prepare().body

print requests.Request('POST', url, data=myobj).prepare().headers



# bets=position&bets=amount&bets=position&bets=amount

# {'Content-Length': '51', 'Content-Type': 'application/x-www-form-urlencoded'}

data使用x-www-form-urlencoded編碼,因此需要一個鍵/值對的平面列表。


您可能想要json表示您的數據:


print requests.Request('POST', url, json=myobj).prepare().body

print requests.Request('POST', url, json=myobj).prepare().headers


# {"bets": [{"position": [0, 1, 2], "amount": 10}, {"position": [10], "amount": 20}]}

# {'Content-Length': '83', 'Content-Type': 'application/json'}

使固定:


x = requests.post(url, json = myobj) // `json` not `data`

最后,值得檢查Content-TypeGo 服務器端的標頭,以確保獲得所需的編碼(在本例中application/json)。


查看完整回答
反對 回復 2022-06-27
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

我認為你需要一個結構來解組數據。我認為這段代碼可以幫助你。


package main


import (

    "encoding/json"

    "fmt"

    "github.com/gorilla/mux"

    "io/ioutil"

    "log"

    "net/http"

)


type Body struct {


    Bets []Persion  `json:"bets"`

}

type Persion struct{

    Amount int  `json:"amount"`

    Position []int  `json:"position"`

}


func handleRequests() {

    // creates a new instance of a mux router

    myRouter := mux.NewRouter().StrictSlash(true)


    myRouter.HandleFunc("/spin/", handler).Methods("POST")

    log.Fatal(http.ListenAndServe(":10000", myRouter))

}


func handler(w http.ResponseWriter, r *http.Request) {


    reqBody, _ := ioutil.ReadAll(r.Body)

     bodyObj :=&Body{}

    err:=json.Unmarshal(reqBody,bodyObj)

    if err!=nil{

        log.Println("%s",err.Error())

    }

    //s := string(reqBody)

    fmt.Println(bodyObj.Bets[0].Amount)

}


func main() {

    fmt.Println("Rest API v2.0 - Mux Routers")

    handleRequests()

}


查看完整回答
反對 回復 2022-06-27
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號