Skip to Content

Golang Code Examples

This page provides complete examples for calling the MeowLoad API with Golang.

🚀 Quick Start

package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" ) func main() { apiURL := "https://api.meowload.net/openapi/extract/post" apiKey := "your-api-key-here" // Build request payload := map[string]string{ "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ", } jsonData, _ := json.Marshal(payload) req, _ := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("x-api-key", apiKey) req.Header.Set("accept-language", "zh") // Send request client := &http.Client{} resp, _ := client.Do(req) defer resp.Body.Close() // Process response body, _ := io.ReadAll(resp.Body) if resp.StatusCode == 200 { fmt.Println("✅ Request successful!") fmt.Println(string(body)) } else { fmt.Printf("❌ Request failed (%d)\n", resp.StatusCode) fmt.Println(string(body)) } }

📦 Full Client Wrapper Example

package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" ) type MeowloadClient struct { APIKey string BaseURL string Client *http.Client } type ExtractRequest struct { URL string `json:"url"` } type Media struct { MediaType string `json:"media_type"` ResourceURL string `json:"resource_url"` PreviewURL string `json:"preview_url,omitempty"` Headers map[string]string `json:"headers,omitempty"` } type ExtractResponse struct { Text string `json:"text,omitempty"` Medias []Media `json:"medias"` ID string `json:"id,omitempty"` CreatedAt string `json:"created_at,omitempty"` } type ErrorResponse struct { Message string `json:"message"` } // NewClient creates a new API client func NewClient(apiKey string) *MeowloadClient { return &MeowloadClient{ APIKey: apiKey, BaseURL: "https://api.meowload.net/openapi", Client: &http.Client{}, } } // ExtractPost extracts a single post func (c *MeowloadClient) ExtractPost(url string) (*ExtractResponse, error) { endpoint := c.BaseURL + "/extract/post" // Build request reqBody := ExtractRequest{URL: url} jsonData, err := json.Marshal(reqBody) if err != nil { return nil, fmt.Errorf("JSON serialization failed: %w", err) } // Create HTTP request req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("failed to create request: %w", err) } // Set request headers req.Header.Set("Content-Type", "application/json") req.Header.Set("x-api-key", c.APIKey) req.Header.Set("accept-language", "zh") // Send request resp, err := c.Client.Do(req) if err != nil { return nil, fmt.Errorf("failed to send request: %w", err) } defer resp.Body.Close() // Read response body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response: %w", err) } // Handle error response if resp.StatusCode != 200 { var errResp ErrorResponse json.Unmarshal(body, &errResp) return nil, fmt.Errorf("request failed (%d): %s", resp.StatusCode, errResp.Message) } // Parse success response var result ExtractResponse if err := json.Unmarshal(body, &result); err != nil { return nil, fmt.Errorf("failed to parse response: %w", err) } return &result, nil } func main() { // Create client client := NewClient("your-api-key-here") // Extract post result, err := client.ExtractPost("https://www.bilibili.com/video/BV1sG4y1p7TA/") if err != nil { fmt.Printf("❌ Error: %v\n", err) return } fmt.Println("✅ Extraction successful!") fmt.Printf("Caption: %s\n", result.Text) fmt.Printf("Media count: %d\n", len(result.Medias)) for idx, media := range result.Medias { fmt.Printf("\nMedia %d:\n", idx+1) fmt.Printf(" Type: %s\n", media.MediaType) fmt.Printf(" URL: %s\n", media.ResourceURL) } }