P
P
Pavel2018-02-14 20:57:46
go
Pavel, 2018-02-14 20:57:46

How to send a get request with a parameter in Russian?

package geogo

import (
  "context"
  "fmt"
  "io/ioutil"
  "net/http"
  "strings"
  //"time"
  "errors"
)

// DefaultTimeout for the request execution
//const DefaultTimeout = time.Second * 8

// ErrTimeout occurs when no response returned within timeoutInSeconds
var ErrTimeout = errors.New("TIMEOUT")

type EndpointBuilder interface {
  GeocodeURL(string) string
}

type HTTPGeocoder struct {
  EndpointBuilder
}

func Response(ctx context.Context, url string) error {
  req, err := http.NewRequest(http.MethodGet, url, nil)
  if err != nil {
    return err
  }
  req = req.WithContext(ctx)

  resp, err := http.DefaultClient.Do(req)
  if err != nil {
    return err
  }

  defer resp.Body.Close()
  data, err := ioutil.ReadAll(resp.Body)
  if err != nil {
    return err
  }

  body := strings.Trim(string(data), " []")

  fmt.Println(url)
  fmt.Println(body)

  if body == "" {
    return nil
  }

  return nil
}

package main

import(
  "context"
  "time"
  "fmt"
  "geogo/geogo"
)

const DefaultTimeout = time.Second * 360


const mapquestAPIKey string = "key"
const searchValue string = "Курган"
const mapquestRequestString string = "https://www.mapquestapi.com/geocoding/v1/address?key="+mapquestAPIKey+"&inFormat=kvp&outFormat=json&location="+searchValue+"&thumbMaps=false" //mapquest string


//Mapquest - проблема с русским языком

func main(){
  ctx, cancel := context.WithTimeout(context.TODO(), DefaultTimeout)
  defer cancel()
  err := geogo.Response(ctx, mapquestRequestString)
  fmt.Println(err)
}

2 files, trying to get data from mapbox.
The result is a query string:

https://www.mapquestapi.com/geocoding/v1/address?k...

if you copy it and execute it in the browser, I will get about 5 points, where the location will be "Barrow"
When executing the go code, I get 1 point, with the location:

location":"\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD"

As far as I understand, the problem is in the encoding, moreover, when sending a request, because from the browser everything is done with a bang. Tell me where to dig? How to fix it?
Approximately according to the same scheme, if I send a request to Google api, I will get the answer I need.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alfss, 2018-02-15
@alfss

URLEncoder.encode(q, "UTF-8");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question