Working with JSON and APIs
The API Integration That Took 3 Days in Python, 4 Hours in Go
import requests
import json
def create_payment(amount, customer_id):
response = requests.post(
"https://api.payment.com/charges",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"amount": amount,
"customer_id": customer_id,
"currency": "USD"
}
)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"API error: {response.text}")JSON Basics
Marshaling (Go → JSON)
Pretty Printing
Unmarshaling (JSON → Go)
Struct Tags in Detail
Basic Tags
Omit Empty Fields
Ignore Fields
String Tags
Custom Field Names
Handling Nested JSON
Nested Structs
Embedded Structs
Arrays and Slices
Maps
Working with http.Client
Basic GET Request
POST Request
Custom Headers
Building a Reusable API Client
Client Structure
Using the Client
Building REST APIs with net/http
Basic HTTP Server
CRUD API Example
Middleware Pattern
Error Handling in HTTP
Real Example: GitHub API Client
Your Challenge
Key Takeaways
What I Learned
Last updated