GoDasturchi
Request Body Parsing

Request Body Parsing

So'rov tanasini o'qish

POST/PUT so'rovlar odatda "tana"da (body) ma'lumot olib keladi — ko'pincha JSON ko'rinishida. Uni o'qish uchun json.NewDecoder(r.Body) ishlatiladi.

example.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
)

type CreateUserRequest struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func createUser(w http.ResponseWriter, r *http.Request) {
	var req CreateUserRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprint(w, "noto'g'ri JSON")
		return
	}
	fmt.Fprintf(w, "yaratildi: %s (%d yosh)", req.Name, req.Age)
}

func main() {
	body := `{"name":"Aziz","age":21}`
	req := httptest.NewRequest("POST", "/users", strings.NewReader(body))
	rec := httptest.NewRecorder()
	createUser(rec, req)
	fmt.Println(rec.Body.String())
}

json.NewDecoder(r.Body).Decode(&req) — so'rov tanasini to'g'ridan-to'g'ri, oraliq []byte yaratmasdan struct'ga o'qiydi. httptest.NewRequestning uchinchi argumenti — sinov uchun so'rov tanasi (io.Reader); haqiqiy serverga kelganda bu — mijoz yuborgan haqiqiy JSON bo'ladi. Noto'g'ri JSON kelsa, Decode xato qaytaradi — buni har doim tekshirish kerak.

>_ Exercise

JSON tanasini o'qib, mahsulot yaratuvchi handler yozing.

  • CreateProductRequest struct'iga mos json tag'lar qo'shing
  • r.Body'ni json.NewDecoder bilan struct'ga dekodlang
  • "mahsulot yaratildi: <name>, narxi: <price>" ko'rinishida javob bering

Stuck? Reveal a hint to help you.

Hints (0/3)

Key Takeaway

Key Takeaway:

json.NewDecoder(r.Body).Decode(&x) — HTTP so'rov tanasidan JSON o'qishning standart, samarali usuli.

NEXT UP

Middleware: Logging and Chaining

OUTPUT

$ go run main.go
Kodingizni ishga tushiring

Request Body Parsing

So'rov tanasini o'qish

POST/PUT so'rovlar odatda "tana"da (body) ma'lumot olib keladi — ko'pincha JSON ko'rinishida. Uni o'qish uchun json.NewDecoder(r.Body) ishlatiladi.

example.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
)

type CreateUserRequest struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func createUser(w http.ResponseWriter, r *http.Request) {
	var req CreateUserRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprint(w, "noto'g'ri JSON")
		return
	}
	fmt.Fprintf(w, "yaratildi: %s (%d yosh)", req.Name, req.Age)
}

func main() {
	body := `{"name":"Aziz","age":21}`
	req := httptest.NewRequest("POST", "/users", strings.NewReader(body))
	rec := httptest.NewRecorder()
	createUser(rec, req)
	fmt.Println(rec.Body.String())
}

json.NewDecoder(r.Body).Decode(&req) — so'rov tanasini to'g'ridan-to'g'ri, oraliq []byte yaratmasdan struct'ga o'qiydi. httptest.NewRequestning uchinchi argumenti — sinov uchun so'rov tanasi (io.Reader); haqiqiy serverga kelganda bu — mijoz yuborgan haqiqiy JSON bo'ladi. Noto'g'ri JSON kelsa, Decode xato qaytaradi — buni har doim tekshirish kerak.

>_ Exercise

JSON tanasini o'qib, mahsulot yaratuvchi handler yozing.

  • CreateProductRequest struct'iga mos json tag'lar qo'shing
  • r.Body'ni json.NewDecoder bilan struct'ga dekodlang
  • "mahsulot yaratildi: <name>, narxi: <price>" ko'rinishida javob bering

Stuck? Reveal a hint to help you.

Hints (0/3)

Key Takeaway

Key Takeaway:

json.NewDecoder(r.Body).Decode(&x) — HTTP so'rov tanasidan JSON o'qishning standart, samarali usuli.

NEXT UP

Middleware: Logging and Chaining