Вы можете попробовать это:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
dataMap := map[string]interface{}{} // own map
data, err := json.Marshal(dataMap)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", "https://newsapi.org/v2/everything", bytes.NewBuffer(data)) // replace url
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := (&http.Client{}).Do(req) // send request
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) // read body
if err != nil {
panic(err)
}
var jsonResp map[string]interface{} // response map
err = json.Unmarshal(body, &jsonResp)
if err != nil {
panic(err)
}
fmt.Printf("Response: %s\nStatus: %s\n", jsonResp, resp.Status)
}
Вывод был:
Ответ: map [status: error code: apiKeyMissing message: Ваш ключ API отсутствует. Добавьте это к URL с параметром apiKey или используйте HTTP-заголовок x-api-key.]
Статус: 401 Несанкционированный
Там все еще есть ошибка, но она вызвана сервером, потому что нет ключа API.