有人可以帮我将红宝石代码转换为Go吗?请参考下面的红宝石代码。
query= "test" request = Net::HTTP::Post.new(url) request.body = query response = Net::HTTP.new(host, post).start{|http http.request(request)}
去。
您似乎想发布查询,该查询类似于此答案:
import ( "bytes" "fmt" "io/ioutil" "net/http" ) func main() { url := "http://xxx/yyy" fmt.Println("URL:>", url) var query = []byte(`your query`) req, err := http.NewRequest("POST", url, bytes.NewBuffer(query)) req.Header.Set("X-Custom-Header", "myvalue") req.Header.Set("Content-Type", "text/plain") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() fmt.Println("response Status:", resp.Status) fmt.Println("response Headers:", resp.Header) body, _ := ioutil.ReadAll(resp.Body) fmt.Println("response Body:", string(body)) }
如果您的查询是JSON查询,请将“ text/plain” 替换为“ ” application/json。
text/plain
application/json