GoDasturchi
Custom HTTP Requests

Custom HTTP Requests

Maxsus HTTP so'rovlari

http.Get faqat GET so'rovi uchun qulay. Sarlavha (header) qo'shish yoki boshqa metod (POST, PUT...) ishlatish kerak bo'lsa — http.NewRequest va http.Client kerak bo'ladi.

example.go
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "auth: %s", r.Header.Get("Authorization"))
	}))
	defer srv.Close()

	req, err := http.NewRequest("GET", srv.URL, nil)
	if err != nil {
		fmt.Println("xato:", err)
		return
	}
	req.Header.Set("Authorization", "Bearer xyz")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("xato:", err)
		return
	}
	defer resp.Body.Close()
	fmt.Println(resp.StatusCode)
}

http.NewRequest(metod, url, tana) so'rovni "tuzadi", lekin hali yubormaydi — bu sizga so'rovga sarlavha qo'shish, o'zgartirish imkonini beradi. req.Header.Set(kalit, qiymat) — masalan avtorizatsiya tokenini qo'shish uchun. client.Do(req) esa tayyorlangan so'rovni haqiqatan yuboradi. &http.Client{} — sozlanishi mumkin bo'lgan klient (masalan timeout qo'shish uchun keyinroq foydali bo'ladi).

>_ Exercise

Maxsus sarlavha bilan so'rov yuboring.

  • http.NewRequest bilan GET so'rovini tuzing
  • "X-Client" sarlavhasiga "go-dasturchi" qiymatini qo'ying
  • client.Do bilan yuboring va javob matnini chop eting

Stuck? Reveal a hint to help you.

Hints (0/3)

Key Takeaway

Key Takeaway:

http.NewRequest + client.Do — sarlavha qo'shish yoki turli HTTP metodlarini ishlatish kerak bo'lganda http.Getdan ko'ra moslashuvchan yo'l.

NEXT UP

Your First HTTP Server

OUTPUT

$ go run main.go
Kodingizni ishga tushiring

Custom HTTP Requests

Maxsus HTTP so'rovlari

http.Get faqat GET so'rovi uchun qulay. Sarlavha (header) qo'shish yoki boshqa metod (POST, PUT...) ishlatish kerak bo'lsa — http.NewRequest va http.Client kerak bo'ladi.

example.go
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "auth: %s", r.Header.Get("Authorization"))
	}))
	defer srv.Close()

	req, err := http.NewRequest("GET", srv.URL, nil)
	if err != nil {
		fmt.Println("xato:", err)
		return
	}
	req.Header.Set("Authorization", "Bearer xyz")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("xato:", err)
		return
	}
	defer resp.Body.Close()
	fmt.Println(resp.StatusCode)
}

http.NewRequest(metod, url, tana) so'rovni "tuzadi", lekin hali yubormaydi — bu sizga so'rovga sarlavha qo'shish, o'zgartirish imkonini beradi. req.Header.Set(kalit, qiymat) — masalan avtorizatsiya tokenini qo'shish uchun. client.Do(req) esa tayyorlangan so'rovni haqiqatan yuboradi. &http.Client{} — sozlanishi mumkin bo'lgan klient (masalan timeout qo'shish uchun keyinroq foydali bo'ladi).

>_ Exercise

Maxsus sarlavha bilan so'rov yuboring.

  • http.NewRequest bilan GET so'rovini tuzing
  • "X-Client" sarlavhasiga "go-dasturchi" qiymatini qo'ying
  • client.Do bilan yuboring va javob matnini chop eting

Stuck? Reveal a hint to help you.

Hints (0/3)

Key Takeaway

Key Takeaway:

http.NewRequest + client.Do — sarlavha qo'shish yoki turli HTTP metodlarini ishlatish kerak bo'lganda http.Getdan ko'ra moslashuvchan yo'l.

NEXT UP

Your First HTTP Server