2017-11-20 1 views
1

Google 지역 API를 통해 앱 범위 내에 장소를 추가하려고합니다. 이것을 위해 저는 골란을 사용하고 있습니다. 하지만 아무런 결과가 나오지 않고 계속 오류 메시지가 표시됩니다.Google 장소 API를 통해 장소를 추가 할 때 비어있는 응답을받습니다.

여기에 내 코드

```다음``

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

나는 골란에 조금 익숙하다. 어떤 도움이 많이 주어질 것이다.

답변

1

FYI 나는이 흥미로운 주제 (JSON 데이터가 Go에서 POST 본문 요청으로 인코딩 됨)에 this article을 작성했습니다.

현재 4 가지를 놓치고 :

  • http.Client 생성. 그런 다음 client.Do을 사용하여 준비중인 요청을 http.NewRequest으로 실행해야합니다.
  • application/jsonContent-Type을 설정
  • 변수의 첫 글자를 대문자로 구조체에 포함하여 구조체 및 수출 변수 json 필드를 추가
  • 구글 대신 types 문자열의 배열을 기대하고, 그래서 배열이 1을 포함하는 교체
  • 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)) 
    } 
    
    : 문자열
  • 여기

가 작동하는 스크립트입니다 (하지만 당신은 Google에 전달하려는 얼마나 많은 종류에 따라이를 적용한다)

희망이 있습니다.

+0

감사합니다. 그러나 이제 응답이 잘못되었습니다. 내가 왜 ok 응답을 못하는지 알 겠어? –

+0

전체 오류를 복사 할 수 있습니까? –

+0

{ "상태": "INVALID_REQUEST" } –

0

내 보낸 필드가없는 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"` 
} 
+0

애드리안에게 감사하지만 여전히 {{ "status": "INVALID_REQUEST" }와 같은 오류가 발생합니다. 콘텐츠 형식을 지정하지 않아서 가능합니까? –

+0

모르겠다면 지정하면 어떻게됩니까? – Adrian

+0

아니, 여전히 같은 오류. –

관련 문제