通过Google Place API添加位置时不断收到空响应

I'm trying to add a place within app scope via google place API. For this I'm using golang. But I keep on getting no result, there is no error message.

Here is my code

```

type latlng struct {
   lat, lng float64
}
type newPlace struct {
   location     latlng
   accuracy     int
   name         string
   phone_number string
   address      string
   types        string
}

func main() {
   requestUrl := "https://maps.googleapis.com/maps/api/place/add/json?key=<MYAPIKEY>"

   obj := newPlace{
      location: latlng{
        lat: 52.1502824,
        lng: 38.2643063,
     },
      name:  "some field",
      types: "storage",
   }

   bodyBytes, err := json.Marshal(&obj)
   if err != nil {
      panic(err)
   }
   body := bytes.NewReader(bodyBytes)
   rsp, err := http.NewRequest("POST", requestUrl, body)
   if err != nil {
       log.Fatal(err)
   }
   defer rsp.Body.Close()

   body_byte, err := ioutil.ReadAll(rsp.Body)
   if err != nil {
       panic(err)
   }
   fmt.Println(string(body_byte))
}

```

Here is the documentation that I followed. https://developers.google.com/places/web-service/add-place

I'm a bit new to golang, any help would be much appreciated.

FYI I wrote this article on this touchy topic (JSON data encoded into a POST body request in Go).

You're missing 4 things here:

  • the http.Client creation. Then you need to execute the request you're preparing with http.NewRequest by using client.Do.
  • add json fields to your struct and export variables contained in struct by capitalizing variables's first letters
  • set Content-Type to application/json
  • Google is expecting an array instead of a string in types, so I replace with an array containing 1 string (but you should adapt this depending on how many types you want to pass to Google)

Here is a working script:

type latlng struct {
    Lat float64 `json:"lat"`
    Lng float64 `json:"lng"`
}

type newPlace struct {
    Location    latlng    `json:"location"`
    Accuracy    int       `json:"accuracy"`
    Name        string    `json:"name"`
    PhoneNumber string    `json:"phone_number"`
    Address     string    `json:"address"`
    Types       [1]string `json:"types"`
}

func main() {
    requestUrl := "https://maps.googleapis.com/maps/api/place/add/json?key=<your key>"

    types := [1]string{"storage"}
    obj := newPlace{
        Location: latlng{
            Lat: 52.1502824,
            Lng: 38.2643063,
        },
        Name:  "some field",
        Types: types,
    }

    bodyBytes, err := json.Marshal(&obj)
    if err != nil {
        fmt.Println(err)
    }
    body := bytes.NewReader(bodyBytes)
    client := &http.Client{}
    req, err := http.NewRequest("POST", requestUrl, body)
    req.Header.Add("Content-Type", "application/json")
    if err != nil {
        fmt.Println(err)
    }
    rsp, err := client.Do(req)
    defer rsp.Body.Close()

    body_byte, err := ioutil.ReadAll(rsp.Body)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(body_byte))
}

Hope it's working now !

You're trying to marshall objects to JSON which have no exported fields, so the resulting JSON document is empty. Per the JSON documentation, it will only marshall exported fields (those whose names begin with a capital letter). Try:

type latlng struct {
   Lat float64 `json:"lat"`
   Lng float64 `json:"lng"`
}

type newPlace struct {
   Location     latlng `json:"location"`
   Accuracy     int `json:"accuracy"`
   Name         string `json:"name"`
   PhoneNumber string `json:"phone_number"`
   Address      string `json:"address"`
   Types        string `json:"types"`
}