通过 google place API 添加 place 时不断收到空响应
Keep getting empty response when adding place via google place API
我正在尝试通过 google 地点 API 在应用程序范围内添加地点。为此,我正在使用 golang。但是我一直没有结果,没有错误消息。
这是我的代码
```
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))
}
```
这是我遵循的文档。 https://developers.google.com/places/web-service/add-place
我对 golang 有点陌生,非常感谢任何帮助。
仅供参考,我在这个敏感话题上写了 this article(JSON 数据在 Go 中编码为 POST 正文请求)。
您在这里遗漏了 4 件事:
http.Client
创作。然后,您需要使用 client.Do
. 来执行您使用 http.NewRequest
准备的请求
- 将
json
字段添加到结构中,并通过大写变量的首字母导出结构中包含的变量
- 将
Content-Type
设置为application/json
- Google 需要一个数组而不是
types
中的一个字符串,所以我用一个包含 1 个字符串的数组替换(但是你应该根据你想要传递给的类型的数量来调整它Google)
这是一个工作脚本:
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))
}
希望它现在可以正常工作了!
您正在尝试将没有导出字段的对象编组到 JSON,因此生成的 JSON 文档是空的。 Per the JSON documentation,它只会编组导出的字段(名称以大写字母开头的字段)。尝试:
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"`
}
我正在尝试通过 google 地点 API 在应用程序范围内添加地点。为此,我正在使用 golang。但是我一直没有结果,没有错误消息。
这是我的代码
```
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))
}
```
这是我遵循的文档。 https://developers.google.com/places/web-service/add-place
我对 golang 有点陌生,非常感谢任何帮助。
仅供参考,我在这个敏感话题上写了 this article(JSON 数据在 Go 中编码为 POST 正文请求)。
您在这里遗漏了 4 件事:
http.Client
创作。然后,您需要使用client.Do
. 来执行您使用 - 将
json
字段添加到结构中,并通过大写变量的首字母导出结构中包含的变量 - 将
Content-Type
设置为application/json
- Google 需要一个数组而不是
types
中的一个字符串,所以我用一个包含 1 个字符串的数组替换(但是你应该根据你想要传递给的类型的数量来调整它Google)
http.NewRequest
准备的请求
这是一个工作脚本:
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))
}
希望它现在可以正常工作了!
您正在尝试将没有导出字段的对象编组到 JSON,因此生成的 JSON 文档是空的。 Per the JSON documentation,它只会编组导出的字段(名称以大写字母开头的字段)。尝试:
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"`
}