从http POST请求解组JSON(Unmarshalling JSON from the http POST request)

编程入门 行业动态 更新时间:2024-10-27 22:26:08
从http POST请求解组JSON(Unmarshalling JSON from the http POST request) python

我有一个用Go编写的简单服务器:

package main import ( "encoding/json" "fmt" "github.com/gorilla/mux" "io/ioutil" "net/http" ) type Game struct { RID string `json: "RID"` Country string `json: "Country"` } func postWaitingGames(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { fmt.Println(err) } var game Game err = json.Unmarshal(body, &game) if err != nil { fmt.Println(err) } fmt.Printf("%v\n", game) defer r.Body.Close() } func main() { router := mux.NewRouter() router.HandleFunc("/wait/", postWaitingGames).Methods("POST") http.ListenAndServe(":8081", router) }

还有一个用Python编写的简单客户端用于测试目的。 这是代码:

import json import requests json_to_send = json.dumps({"RID": "8", "Country": "Australia"}) post_headers = {'Content-type': 'application/json'} addr = "http://127.0.0.1:8081/wait/" resp = requests.post(url=addr, json=json_to_send, headers=post_headers) print(resp.status_code)

每次客户端命中服务器时,后者都会产生以下错误:

json: cannot unmarshal string into Go value of type main.Game

我知道在Go中处理JSON Post Request

Python版本== 3.4 Go版本== 1.7

先谢谢你。

I have a simple server written in Go:

package main import ( "encoding/json" "fmt" "github.com/gorilla/mux" "io/ioutil" "net/http" ) type Game struct { RID string `json: "RID"` Country string `json: "Country"` } func postWaitingGames(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { fmt.Println(err) } var game Game err = json.Unmarshal(body, &game) if err != nil { fmt.Println(err) } fmt.Printf("%v\n", game) defer r.Body.Close() } func main() { router := mux.NewRouter() router.HandleFunc("/wait/", postWaitingGames).Methods("POST") http.ListenAndServe(":8081", router) }

And a simple client written in Python for testing purposes. Here's the code:

import json import requests json_to_send = json.dumps({"RID": "8", "Country": "Australia"}) post_headers = {'Content-type': 'application/json'} addr = "http://127.0.0.1:8081/wait/" resp = requests.post(url=addr, json=json_to_send, headers=post_headers) print(resp.status_code)

and everytime the client hits the server, the latter yields this error:

json: cannot unmarshal string into Go value of type main.Game

I'm aware of Handling JSON Post Request in Go

Python version == 3.4 Go version == 1.7

Thank you in advance.

最满意答案

如果使用requests.post()的json参数,你必须传递Python dict ,而不是json序列化版本 - requests将负责调用json.dumps() 。 在这里你最终将你的dict序列化了两次。

此外 - 总是在使用json参数时 - 您不需要设置内容类型标头, requests也将处理此问题。

If using requests.post()'s json argument, you must pass you Python dict, not the json-serialized version -requests will take care of calling json.dumps() on it. Here you end up having your dict serialized twice.

Also - always when using the json argument - you don't need to set the content-type header, requests will take care of this too.

更多推荐

本文发布于:2023-08-05 05:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:POST   http   JSON   request   Unmarshalling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!