Skip to main content

Go Örnekleri

Go (net/http) ile API istekleri yapmak için örnekler.

Temel Kullanım

GET İsteği

package main

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

func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://api.example.com/api/v1/users", nil)
req.Header.Set("Authorization", "Bearer YOUR_TOKEN")
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

var data map[string]interface{}
json.NewDecoder(resp.Body).Decode(&data)
fmt.Println(data)
}

POST İsteği

import (
"bytes"
"encoding/json"
"net/http"
)

type User struct {
Name string `json:"name"`
Email string `json:"email"`
}

user := User{Name: "John Doe", Email: "john@example.com"}
jsonData, _ := json.Marshal(user)

req, _ := http.NewRequest("POST", "https://api.example.com/api/v1/users", bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "Bearer YOUR_TOKEN")
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)

Login Örneği

loginData := map[string]string{
"email": "admin@example.com",
"password": "secret123",
"device_name": "MyDevice",
}

jsonData, _ := json.Marshal(loginData)
req, _ := http.NewRequest("POST", "https://api.example.com/api/v1/login", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)

İlgili Dokümantasyon