Запросы к REST API на Go

Содержание
Введение
GET
POST
PUT
DELETE
Другие статьи о Go

Введение

GET

package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://example.com/engine/api/v1/wfs" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("accept", "application/json") req.Header.Add("Authorization", "Bearer a-proper-token-goes-here") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }

POST

package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://example.com/engine/api/v1/wfs" req, _ := http.NewRequest("POST", url, nil) req.Header.Add("accept", "application/json") req.Header.Add("Authorization", "Bearer a-proper-token-goes-here") req.Header.Add("content-type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }

PUT

package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://example.com/engine/api/v1/wfs/wf_id" req, _ := http.NewRequest("PUT", url, nil) req.Header.Add("accept", "application/json") req.Header.Add("Authorization", "Bearer a-proper-token-goes-here") req.Header.Add("content-type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }

DELETE

package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://example.com/engine/api/v1/wfs/wf_id" req, _ := http.NewRequest("DELETE", url, nil) req.Header.Add("accept", "application/json") req.Header.Add("Authorization", "Bearer a-proper-token-goes-here") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }

Article author: Andrei Olegovich

Похожие статьи
Программирование на Go
Объявление переменных
Определить тип переменной
Указатели
Константы
Сертификаты
Запросы к REST API на Go
Установка в Linux
REST

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: