go post json 遇到的问题

想用go写一个客户端,发一个JSON数据到JAVA的服务端接口,

结果,JAVA接收的BODY一直是空,但是PYTHON写的服务端是OK的

网上查了方法

其中:一些方法 也试过了,

type RequestBody struct {

Status  string   `json:"status"`

Region  []string `json:"region"`

Percent string   `json:"percent"`

Task_id string   `json:"task_id"`

}

b, err := json.Marshal(rbody)

body := bytes.NewBuffer([]byte(b))

(1)

resp, err := http.Post(url, "application/json", body)

(2)

resp, err := http.Post(url, "application/x-www-form-urlencoded", body)


(3)

client := &http.Client{}

req, _ := http.NewRequest("POST", url, body)

req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)

都不行,最后,尝试一下,将

req, _ := http.NewRequest("POST", url, body)

改为

req, _ := http.NewRequest("POST", url, strings.NewReader(string(b)))

OK了


你可能感兴趣的:(go post json 遇到的问题)