我可以使用 POSTMAN chrome 扩展对字符串数据执行 POST 请求。
我需要使用golang代码做同样的事情。
但是我的Go代码丢失了字符串INSERT INTO V SET name = 'jack', boss = #11:19并将空数据发布到服务器。
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
func main() {
client := &http.Client{}
// Why this stringData is lost and was not send with POST request?
stringData := `INSERT INTO V SET name = 'jack', boss = #11:19`
req, err := http.NewRequest("POST", "http://localhost:2480/command/GratefulDeadConcerts/sql", bytes.NewBufferString(stringData))
req.SetBasicAuth("root", "1")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error : %s", err)
}
fmt.Println("resp")
fmt.Println(ToJson(resp))
var b bytes.Buffer
_, err = b.ReadFrom(resp.Body)
if err != nil {
log.Fatal("Error : %s", err)
}
fmt.Println(b.String())
}
func ToJson(obj interface{}) string {
b, err := json.MarshalIndent(&obj, "", " ")
if err != nil {
fmt.Printf("Error : %s", err)
}
strJson := string(b)
return strJson
}
相关分类