M
M
Master Ruby2021-09-03 19:39:16
go
Master Ruby, 2021-09-03 19:39:16

How to set the encoding on request?

This is a request to the page to get a JSON object.

package main

import (
  "io/ioutil"
  "log"
  "net/http"
)

func main() {
  MakeRequest()
}

func MakeRequest() {

  resp, err := http.Get("http//example.com")
  if err != nil {
    log.Fatal(err)
  }

  body, err := ioutil.ReadAll(resp.Body)
  if err != nil {
    log.Fatal(err)
  }
  log.Println(string(body))
}

Outputs text, both normal and "u043e\u0443\u0446\u043a\u043e".
Perhaps you need to set the encoding?

OS: Windows

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-09-03
@Dunaevlad

Perhaps you need to set the encoding?

No, this is a feature of the site to which the request is being made and cannot be influenced.
This is how in js you can encode Unicode characters - in this case, Cyrillic.
Written there "ootsko"
To decode json, you should use the appropriate module:
https://pkg.go.dev/encoding/json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question