Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问POST请求中的嵌入键值_Python_Rest_Go_Post - Fatal编程技术网

Python 如何访问POST请求中的嵌入键值

Python 如何访问POST请求中的嵌入键值,python,rest,go,post,Python,Rest,Go,Post,我正在Golang制作一个轮盘赌REST API: package main import ( "fmt" "io/ioutil" "log" "net/http" "github.com/gorilla/mux" ) func handleRequests() { // creates a new instance of a mux route

我正在Golang制作一个轮盘赌REST API:

package main

import (
    "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()
}
梅因,加油

我正在用Python脚本测试POST方法:

import requests

url = '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请求。请求机构是:
下注=金额&下注=头寸&下注=金额&下注=头寸

问题是
'amount'
'position'
键的值不存在


我的问题是-我如何发出/处理POST请求,以便能够访问Go服务器上我的处理程序函数中嵌入的键
'amount'
'position'
的值,这样我就可以把这些信息放到一个结构的实例中。

我想你需要一个结构来解组数据。我想这段代码可以帮助你

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()
}

问题在于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'}
修正:

最后,值得检查Go服务器端的
内容类型
头,以确保获得预期的编码(在本例中为
应用程序/json

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