how do i create a post request like this in golang

huangapple go评论86阅读模式
英文:

how do i create a post request like this in golang

问题

大家好,我有一个关于POST请求的问题。

我有一段类似这样的Python代码:
data = {
"name": "Frank",
"age": 21,
"nationality": ["Britan"],
}

r = requests.post('somesite', json=data)

我想知道如何在Golang中进行类似的POST请求。我尝试使用"nationality": ["Britan"],但是遇到了一些关于[]的错误。我尝试使用map[string]string,但是当然不起作用。也许我可以使用一些结构来解决我的问题。

英文:

Hi guys a have a question about POST REQUEST

I have a some python code like this
data = {
"name": "Frank",
"age": 21,
"nationality": ["Britan"],
}

r = requests.post('somesite', json=data)
How i can make a POST requst similar this on GOLANG, i tried use "nationality": ["Britan"]
but i have a some errors with []
i tried to use map[string]string but ofc its not working
May be i can use some structure to resolve my problem

答案1

得分: 1

也许你应该使用map[string]interface{}

或者你也可以使用strings.NewReader直接发送请求。

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://localhost:8080"
  method := "POST"

  payload := strings.NewReader(`{
    "name": "Flank",
    "age": 21,
    "nationality": ["Britan"]
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
英文:

maybe you should use map[string]interface{}

or you can also use strings.NewReader to send request directly

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://localhost:8080"
  method := "POST"

  payload := strings.NewReader(`{
    "name": "Flank",
    "age": 21,
    "nationality": ["Britan"]
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}

答案2

得分: 0

你应该使用map[string]interface{}而不是map[string]string。

根据这个链接(https://go.dev/blog/maps):

map[KeyType]ValueType

其中KeyType可以是任何可比较的类型,而ValueType可以是任何类型,包括另一个map!

你的数据结构中既有string类型又有slice类型,所以ValueType最好使用interface{}而不是string。

英文:

You should use map[string]interface{} instead of map[string]string

As this link (https://go.dev/blog/maps):

map[KeyType]ValueType

where KeyType may be any type that is comparable, and ValueType may be any type at all, including another map!

Your body has both string and slice type, so ValueType is interface{} better than string.

huangapple
  • 本文由 发表于 2022年6月26日 04:38:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72757209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定