محول Curl إلى Go

تحويل أوامر curl إلى كود Go - توليد كود Go net/http جاهز للاستخدام لطلبات API

إشعار الخصوصية: توفر هذه الأداة الاحترافية تحويلاً آمناً إلى كود Go مع حماية خصوصية على مستوى المؤسسات. نحن لا نخزن أي بيانات تقدمها، مما يضمن سرية كاملة لعمل تطوير API الخاص بك.

مولد كود Go

// Go code will appear here
// Example:
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	url := "https://api.example.com/data"
	
	// Create request payload
	payload := map[string]interface{}{
		"name": "test",
	}
	
	jsonData, err := json.Marshal(payload)
	if err != nil {
		fmt.Println("Error marshaling JSON:", err)
		return
	}
	
	// Create request
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}
	
	// Add headers
	req.Header.Set("Content-Type", "application/json")
	
	// Send request
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()
	
	// Read response
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return
	}
	
	fmt.Println("Status:", resp.Status)
	fmt.Println("Response:", string(body))
}

أوامر curl الشائعة لاختبار API في Go

فيما يلي بعض أوامر curl الشائعة التي يمكنك تحويلها إلى كود Go:

أمثلة HTTP في Go

توفر حزمة net/http في Go طريقة قوية وفعالة لإجراء طلبات HTTP. فيما يلي بعض أنماط HTTP الشائعة في Go:

تحميل الملفات باستخدام Go

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
	"path/filepath"
)

func main() {
	url := "https://api.example.com/upload"
	
	// Create a buffer to store the multipart form
	var requestBody bytes.Buffer
	multipartWriter := multipart.NewWriter(&requestBody)
	
	// Open the file
	file, err := os.Open("document.pdf")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()
	
	// Create a form file field
	fileWriter, err := multipartWriter.CreateFormFile("file", filepath.Base("document.pdf"))
	if err != nil {
		fmt.Println("Error creating form file:", err)
		return
	}
	
	// Copy the file content to the form field
	_, err = io.Copy(fileWriter, file)
	if err != nil {
		fmt.Println("Error copying file to form:", err)
		return
	}
	
	// Add other form fields if needed
	multipartWriter.WriteField("description", "Sample document upload")
	
	// Close the multipart writer
	multipartWriter.Close()
	
	// Create request
	req, err := http.NewRequest("POST", url, &requestBody)
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}
	
	// Set the content type with the boundary
	req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
	req.Header.Set("Authorization", "Bearer YOUR_TOKEN_HERE")
	
	// Send request
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()
	
	// Read response
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return
	}
	
	fmt.Println("Status:", resp.Status)
	fmt.Println("Response:", string(body))
}

HTTP في Go مع المهلة ومعالجة الأخطاء

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func main() {
	// Create a context with timeout
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	
	// Create request
	req, err := http.NewRequestWithContext(ctx, "GET", "https://api.example.com/data", nil)
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}
	
	// Send request
	client := &http.Client{}
	resp, err := client.Do(req)
	
	// Handle errors
	if err != nil {
		if ctx.Err() == context.DeadlineExceeded {
			fmt.Println("Request timed out")
		} else {
			fmt.Println("Error sending request:", err)
		}
		return
	}
	defer resp.Body.Close()
	
	// Check status code
	if resp.StatusCode != http.StatusOK {
		fmt.Printf("Server returned non-200 status: %s\n", resp.Status)
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println("Response body:", string(body))
		return
	}
	
	// Read and parse response
	var data map[string]interface{}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return
	}
	
	err = json.Unmarshal(body, &data)
	if err != nil {
		fmt.Println("Error parsing JSON:", err)
		fmt.Println("Raw response:", string(body))
		return
	}
	
	fmt.Println("Successfully received data:", data)
}

كيفية استخدام محول كود Go

1. الاستخدام الأساسي

انسخ أمر curl الخاص بك → الصقه في مربع الإدخال → احصل على كود Go فوراً

2. تنفيذ معالجة الأخطاء بشكل صحيح

// Always check for errors in Go
resp, err := client.Do(req)
if err != nil {
	fmt.Println("Error sending request:", err)
	return
}
defer resp.Body.Close()

// Check status code
if resp.StatusCode != http.StatusOK {
	fmt.Printf("Server returned non-200 status: %s\n", resp.Status)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("Response body:", string(body))
	return
}

3. استخدام Context للمهلات

// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// Create request with context
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.example.com/data", nil)
if err != nil {
	fmt.Println("Error creating request:", err)
	return
}

// Send request
client := &http.Client{}
resp, err := client.Do(req)

// Check for timeout
if err != nil {
	if ctx.Err() == context.DeadlineExceeded {
		fmt.Println("Request timed out")
	} else {
		fmt.Println("Error sending request:", err)
	}
	return
}

الأسئلة الشائعة حول طلبات HTTP في Go

س: كيف يختلف عميل HTTP في Go عن curl؟

ج: بينما curl هي أداة سطر أوامر لإجراء طلبات HTTP، توفر حزمة net/http في Go واجهة برمجة تطبيقات برمجية. يوفر Go كتابة أقوى، ودعم تزامن مدمج، وتكامل أفضل مع كود التطبيق الخاص بك، بينما يتفوق curl في الاختبار السريع المخصص والبرمجة النصية.

س: لماذا يستخدم Go defer لإغلاق أجسام الاستجابة؟

ج: يستخدم Go defer resp.Body.Close() لضمان تحرير الموارد بشكل صحيح حتى إذا حدثت أخطاء لاحقاً في الدالة. هذا النمط هو نمط مميز في Go ويمنع تسرب الموارد الذي قد يحدث إذا نسيت إغلاق الجسم في جميع مسارات الكود المحتملة.

س: كيف أتعامل مع استجابات التدفق في Go؟

ج: بدلاً من استخدام ioutil.ReadAll()، يمكنك معالجة جسم الاستجابة كتدفق باستخدام io.Copy() أو بقراءة أجزاء باستخدام مخزن مؤقت. هذا مفيد بشكل خاص للاستجابات الكبيرة حيث يكون تحميل الجسم بأكمله إلى الذاكرة غير فعال.

س: هل يمكنني إعادة استخدام عملاء HTTP في Go؟

ج: نعم، وهو أمر موصى به! إنشاء http.Client واحد وإعادة استخدامه عبر الطلبات يسمح بتجميع الاتصالات ويحسن الأداء. العميل آمن للتزامن ويمكن مشاركته بين goroutines دون الحاجة إلى تزامن إضافي.

س: كيف أنفذ إلغاء الطلب في Go؟

ج: توفر حزمة context في Go إلغاء طلب أنيق. قم بإنشاء سياق باستخدام context.WithCancel() أو context.WithTimeout()، ثم مرره إلى http.NewRequestWithContext(). يمكنك إلغاء الطلب في أي وقت عن طريق استدعاء دالة الإلغاء.

س: ما هو المكافئ لعلامة curl -k/--insecure في Go؟

ج: لتخطي التحقق من شهادة TLS (مثل علامة -k في curl)، قم بتكوين Transport مخصص في عميل HTTP الخاص بك: client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}} ومع ذلك، يجب استخدام هذا للاختبار فقط، لأنه يتجاوز فحوصات الأمان.

س: كيف أتعامل مع إعادة التوجيه في Go مقارنة بـ curl؟

ج: يتبع عميل HTTP في Go إعادة التوجيه تلقائياً (حتى 10 مرات افتراضياً)، مشابهاً لـ curl. لتخصيص هذا السلوك، قم بتعيين دالة CheckRedirect مخصصة في عميل HTTP الخاص بك للتحكم في معالجة إعادة التوجيه أو منع إعادة التوجيه تماماً.

مرجع أوامر Curl لاختبار API في Go

فهم أوامر curl ضروري لاختبار API الفعال مع Go. إليك مرجعاً سريعاً لخيارات curl الشائعة التي يدعمها محولنا:

بناء جملة curl الأساسي

curl [options] [URL]

خيارات curl الشائعة

تحويل أوامر curl المعقدة

يتعامل محول Go لدينا مع أوامر curl المعقدة بما في ذلك الرؤوس المتعددة والمصادقة وحمولات البيانات والخيارات المختلفة. ما عليك سوى لصق أمر curl الخاص بك والحصول على كود Go نظيف وحديث باستخدام حزمة net/http.

أفضل ممارسات HTTP في Go

عند العمل مع حزمة net/http في Go، اتبع أفضل الممارسات التالية للتفاعلات الفعالة والآمنة مع API:

1. إغلاق جسم الاستجابة دائماً

resp, err := client.Do(req)
if err != nil {
	fmt.Println("Error sending request:", err)
	return
}
defer resp.Body.Close() // Important: prevents resource leaks

2. استخدام عميل HTTP مخصص

client := &http.Client{
	Timeout: 10 * time.Second,
	Transport: &http.Transport{
		MaxIdleConns:        100,
		MaxIdleConnsPerHost: 20,
		IdleConnTimeout:     90 * time.Second,
	},
}

resp, err := client.Do(req)
if err != nil {
	fmt.Println("Error sending request:", err)
	return
}

3. تنفيذ معالجة شاملة للأخطاء

resp, err := client.Do(req)
if err != nil {
	var netErr net.Error
	if errors.As(err, &netErr) && netErr.Timeout() {
		fmt.Println("Request timed out")
	} else if errors.Is(err, context.DeadlineExceeded) {
		fmt.Println("Context deadline exceeded")
	} else {
		fmt.Println("Error sending request:", err)
	}
	return
}
defer resp.Body.Close()

// Check status code
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Printf("Error status: %d %s\n", resp.StatusCode, resp.Status)
	fmt.Printf("Response body: %s\n", string(body))
	return
}

// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
	fmt.Println("Error reading response body:", err)
	return
}

// Process response data
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
	fmt.Println("Error parsing JSON:", err)
	fmt.Println("Raw response:", string(body))
	return
}

4. استخدام Context للتحكم في المهلة والإلغاء

// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() // Important: prevents context leaks

// Create request with context
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.example.com/data", nil)
if err != nil {
	fmt.Println("Error creating request:", err)
	return
}

// Send request
resp, err := client.Do(req)
// Error handling...

5. استخدام Structs لتسلسل وتحليل JSON

// Define request and response structs
type User struct {
	ID    int    `json:"id,omitempty"`
	Name  string `json:"name"`
	Email string `json:"email"`
}

// Create request data
user := User{
	Name:  "John Doe",
	Email: "[email protected]",
}

// Serialize to JSON
jsonData, err := json.Marshal(user)
if err != nil {
	fmt.Println("Error marshaling JSON:", err)
	return
}

// Create request
req, err := http.NewRequest("POST", "https://api.example.com/users", bytes.NewBuffer(jsonData))
// Set headers, send request, etc...

// Deserialize response
var responseUser User
if err := json.Unmarshal(body, &responseUser); err != nil {
	fmt.Println("Error parsing JSON response:", err)
	return
}
fmt.Printf("Created user with ID: %d\n", responseUser.ID)