مكتبة HTTP و REST بسيطة لـ GO (مستوحاة من Ruby Rest-Client)
يصف قسم الميزات بالتفصيل عن قدرات RESTY

string ، []byte ، struct ، map ، slice و io.Reader أيضًاContent-Typeio.Reader*http.Request الأصليين أثناء البرامج الوسيطة وتنفيذ الطلب عبر Request.RawRequestRequest.RawRequest.GetBody()string []byte صفيف - response.Body() response.String()response.Time() وعندما response.ReceivedAt()JSON و XMLJSON ، إذا قمت بتزويد struct/map بدون Content-Type الرأسapplication/problem+json application/problem+xmlmultipart/form-data أو أكثرRequest.SetContext مدعومBearer BasicAuthContentLength لجميع الطلبات أو طلب معينcurl -o . انظر setotutputdirectory & setoutput.Timeout ، RedirectPolicy ، Proxy ، TLSClientConfig ، Transport ، إلخ.Content-Type المتوقع عند مفقود رأس Content-Type الاستجابة. الرجوع إلى #92resty.New()http.RoundTripper ، انظر setTransportRequestAttempt ، ويحتوي كائن Request على سمة AttemptHTTP/2 و HTTP/1.1 ، كما يمكن استخدام HTTP/3 مع RESTY ، انظر هذا التعليق الموصى بها لاستخدام go1.20 وما فوق.
بدأت Resty في البداية في دعم go modules منذ الإصدار v1.10.0 .
بدء Resty V2 والإصدارات الأعلى ، فإنه يحتضن تمامًا إصدار حزمة MO GO. يتطلب إصدار GO قادرًا على فهم /vN واردات اللاحقة:
كما نشر Resty Auther مشاريع التالية لمجتمع Go.
struct GO. # Go Modules
require github.com/go-resty/resty/v2 v2.16.2سوف تساعدك العينات التالية على أن تصبح مريحة قدر الإمكان مع مكتبة Resty.
// Import resty into your code and refer it as `resty`.
import "github.com/go-resty/resty/v2" // Create a Resty Client
client := resty . New ()
resp , err := client . R ().
EnableTrace ().
Get ( "https://httpbin.org/get" )
// Explore response object
fmt . Println ( "Response Info:" )
fmt . Println ( " Error :" , err )
fmt . Println ( " Status Code:" , resp . StatusCode ())
fmt . Println ( " Status :" , resp . Status ())
fmt . Println ( " Proto :" , resp . Proto ())
fmt . Println ( " Time :" , resp . Time ())
fmt . Println ( " Received At:" , resp . ReceivedAt ())
fmt . Println ( " Body : n " , resp )
fmt . Println ()
// Explore trace info
fmt . Println ( "Request Trace Info:" )
ti := resp . Request . TraceInfo ()
fmt . Println ( " DNSLookup :" , ti . DNSLookup )
fmt . Println ( " ConnTime :" , ti . ConnTime )
fmt . Println ( " TCPConnTime :" , ti . TCPConnTime )
fmt . Println ( " TLSHandshake :" , ti . TLSHandshake )
fmt . Println ( " ServerTime :" , ti . ServerTime )
fmt . Println ( " ResponseTime :" , ti . ResponseTime )
fmt . Println ( " TotalTime :" , ti . TotalTime )
fmt . Println ( " IsConnReused :" , ti . IsConnReused )
fmt . Println ( " IsConnWasIdle :" , ti . IsConnWasIdle )
fmt . Println ( " ConnIdleTime :" , ti . ConnIdleTime )
fmt . Println ( " RequestAttempt:" , ti . RequestAttempt )
fmt . Println ( " RemoteAddr :" , ti . RemoteAddr . String ())
/* Output
Response Info:
Error : <nil>
Status Code: 200
Status : 200 OK
Proto : HTTP/2.0
Time : 457.034718ms
Received At: 2020-09-14 15:35:29.784681 -0700 PDT m=+0.458137045
Body :
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "httpbin.org",
"User-Agent": "go-resty/2.4.0 (https://github.com/go-resty/resty)",
"X-Amzn-Trace-Id": "Root=1-5f5ff031-000ff6292204aa6898e4de49"
},
"origin": "0.0.0.0",
"url": "https://httpbin.org/get"
}
Request Trace Info:
DNSLookup : 4.074657ms
ConnTime : 381.709936ms
TCPConnTime : 77.428048ms
TLSHandshake : 299.623597ms
ServerTime : 75.414703ms
ResponseTime : 79.337µs
TotalTime : 457.034718ms
IsConnReused : false
IsConnWasIdle : false
ConnIdleTime : 0s
RequestAttempt: 1
RemoteAddr : 3.221.81.55:443
*/ // Create a Resty Client
client := resty . New ()
resp , err := client . R ().
SetQueryParams ( map [ string ] string {
"page_no" : "1" ,
"limit" : "20" ,
"sort" : "name" ,
"order" : "asc" ,
"random" : strconv . FormatInt ( time . Now (). Unix (), 10 ),
}).
SetHeader ( "Accept" , "application/json" ).
SetAuthToken ( "BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F" ).
Get ( "/search_result" )
// Sample of using Request.SetQueryString method
resp , err := client . R ().
SetQueryString ( "productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more" ).
SetHeader ( "Accept" , "application/json" ).
SetAuthToken ( "BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F" ).
Get ( "/show_product" )
// If necessary, you can force response content type to tell Resty to parse a JSON response into your struct
resp , err := client . R ().
SetResult ( result ).
ForceContentType ( "application/json" ).
Get ( "v2/alpine/manifests/latest" ) // Create a Resty Client
client := resty . New ()
// POST JSON string
// No need to set content type, if you have client level setting
resp , err := client . R ().
SetHeader ( "Content-Type" , "application/json" ).
SetBody ( `{"username":"testuser", "password":"testpass"}` ).
SetResult ( & AuthSuccess {}). // or SetResult(AuthSuccess{}).
Post ( "https://myapp.com/login" )
// POST []byte array
// No need to set content type, if you have client level setting
resp , err := client . R ().
SetHeader ( "Content-Type" , "application/json" ).
SetBody ([] byte ( `{"username":"testuser", "password":"testpass"}` )).
SetResult ( & AuthSuccess {}). // or SetResult(AuthSuccess{}).
Post ( "https://myapp.com/login" )
// POST Struct, default is JSON content type. No need to set one
resp , err := client . R ().
SetBody ( User { Username : "testuser" , Password : "testpass" }).
SetResult ( & AuthSuccess {}). // or SetResult(AuthSuccess{}).
SetError ( & AuthError {}). // or SetError(AuthError{}).
Post ( "https://myapp.com/login" )
// POST Map, default is JSON content type. No need to set one
resp , err := client . R ().
SetBody ( map [ string ] interface {}{ "username" : "testuser" , "password" : "testpass" }).
SetResult ( & AuthSuccess {}). // or SetResult(AuthSuccess{}).
SetError ( & AuthError {}). // or SetError(AuthError{}).
Post ( "https://myapp.com/login" )
// POST of raw bytes for file upload. For example: upload file to Dropbox
fileBytes , _ := os . ReadFile ( "/Users/jeeva/mydocument.pdf" )
// See we are not setting content-type header, since go-resty automatically detects Content-Type for you
resp , err := client . R ().
SetBody ( fileBytes ).
SetContentLength ( true ). // Dropbox expects this value
SetAuthToken ( "<your-auth-token>" ).
SetError ( & DropboxError {}). // or SetError(DropboxError{}).
Post ( "https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf" ) // for upload Dropbox supports PUT too
// Note: resty detects Content-Type for request body/payload if content type header is not set.
// * For struct and map data type defaults to 'application/json'
// * Fallback is plain text content type يمكنك استخدام مجموعات مختلفة من استدعاء طريقة PUT كما هو موضح POST .
// Note: This is one sample of PUT method usage, refer POST for more combination
// Create a Resty Client
client := resty . New ()
// Request goes as JSON content type
// No need to set auth token, error, if you have client level settings
resp , err := client . R ().
SetBody ( Article {
Title : "go-resty" ,
Content : "This is my article content, oh ya!" ,
Author : "Jeevanandam M" ,
Tags : [] string { "article" , "sample" , "resty" },
}).
SetAuthToken ( "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ).
SetError ( & Error {}). // or SetError(Error{}).
Put ( "https://myapp.com/article/1234" ) يمكنك استخدام مجموعات مختلفة من استدعاء طريقة PATCH كما هو موضح POST .
// Note: This is one sample of PUT method usage, refer POST for more combination
// Create a Resty Client
client := resty . New ()
// Request goes as JSON content type
// No need to set auth token, error, if you have client level settings
resp , err := client . R ().
SetBody ( Article {
Tags : [] string { "new tag1" , "new tag2" },
}).
SetAuthToken ( "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ).
SetError ( & Error {}). // or SetError(Error{}).
Patch ( "https://myapp.com/articles/1234" ) // Create a Resty Client
client := resty . New ()
// DELETE a article
// No need to set auth token, error, if you have client level settings
resp , err := client . R ().
SetAuthToken ( "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ).
SetError ( & Error {}). // or SetError(Error{}).
Delete ( "https://myapp.com/articles/1234" )
// DELETE a articles with payload/body as a JSON string
// No need to set auth token, error, if you have client level settings
resp , err := client . R ().
SetAuthToken ( "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ).
SetError ( & Error {}). // or SetError(Error{}).
SetHeader ( "Content-Type" , "application/json" ).
SetBody ( `{article_ids: [1002, 1006, 1007, 87683, 45432] }` ).
Delete ( "https://myapp.com/articles" )
// HEAD of resource
// No need to set auth token, if you have client level settings
resp , err := client . R ().
SetAuthToken ( "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ).
Head ( "https://myapp.com/videos/hi-res-video" )
// OPTIONS of resource
// No need to set auth token, if you have client level settings
resp , err := client . R ().
SetAuthToken ( "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ).
Options ( "https://myapp.com/servers/nyc-dc-01" ) يمكن للمستخدم تسجيل اختيار مكتبة JSON/XML في RESTY أو الكتابة الخاصة بك. بشكل افتراضي ، يسجل Resty encoding/json القياسي encoding/xml على التوالي.
// Example of registering json-iterator
import jsoniter "github.com/json-iterator/go"
json := jsoniter . ConfigCompatibleWithStandardLibrary
client := resty . New ().
SetJSONMarshaler ( json . Marshal ).
SetJSONUnmarshaler ( json . Unmarshal )
// similarly user could do for XML too with -
client . SetXMLMarshaler ( xml . Marshal ).
SetXMLUnmarshaler ( xml . Unmarshal ) profileImgBytes , _ := os . ReadFile ( "/Users/jeeva/test-img.png" )
notesBytes , _ := os . ReadFile ( "/Users/jeeva/text-file.txt" )
// Create a Resty Client
client := resty . New ()
resp , err := client . R ().
SetFileReader ( "profile_img" , "test-img.png" , bytes . NewReader ( profileImgBytes )).
SetFileReader ( "notes" , "text-file.txt" , bytes . NewReader ( notesBytes )).
SetFormData ( map [ string ] string {
"first_name" : "Jeevanandam" ,
"last_name" : "M" ,
}).
Post ( "http://myapp.com/upload" ) // Create a Resty Client
client := resty . New ()
// Single file scenario
resp , err := client . R ().
SetFile ( "profile_img" , "/Users/jeeva/test-img.png" ).
Post ( "http://myapp.com/upload" )
// Multiple files scenario
resp , err := client . R ().
SetFiles ( map [ string ] string {
"profile_img" : "/Users/jeeva/test-img.png" ,
"notes" : "/Users/jeeva/text-file.txt" ,
}).
Post ( "http://myapp.com/upload" )
// Multipart of form fields and files
resp , err := client . R ().
SetFiles ( map [ string ] string {
"profile_img" : "/Users/jeeva/test-img.png" ,
"notes" : "/Users/jeeva/text-file.txt" ,
}).
SetFormData ( map [ string ] string {
"first_name" : "Jeevanandam" ,
"last_name" : "M" ,
"zip_code" : "00001" ,
"city" : "my city" ,
"access_token" : "C6A79608-782F-4ED0-A11D-BD82FAD829CD" ,
}).
Post ( "http://myapp.com/profile" ) // Create a Resty Client
client := resty . New ()
// just mentioning about POST as an example with simple flow
// User Login
resp , err := client . R ().
SetFormData ( map [ string ] string {
"username" : "jeeva" ,
"password" : "mypass" ,
}).
Post ( "http://myapp.com/login" )
// Followed by profile update
resp , err := client . R ().
SetFormData ( map [ string ] string {
"first_name" : "Jeevanandam" ,
"last_name" : "M" ,
"zip_code" : "00001" ,
"city" : "new city update" ,
}).
Post ( "http://myapp.com/profile" )
// Multi value form data
criteria := url. Values {
"search_criteria" : [] string { "book" , "glass" , "pencil" },
}
resp , err := client . R ().
SetFormDataFromValues ( criteria ).
Post ( "http://myapp.com/search" ) // Create a Resty Client
client := resty . New ()
// Setting output directory path, If directory not exists then resty creates one!
// This is optional one, if you're planning using absolute path in
// `Request.SetOutput` and can used together.
client . SetOutputDirectory ( "/Users/jeeva/Downloads" )
// HTTP response gets saved into file, similar to curl -o flag
_ , err := client . R ().
SetOutput ( "plugin/ReplyWithHeader-v5.1-beta.zip" ).
Get ( "http://bit.ly/1LouEKr" )
// OR using absolute path
// Note: output directory path is not used for absolute path
_ , err := client . R ().
SetOutput ( "/MyDownloads/plugin/ReplyWithHeader-v5.1-beta.zip" ).
Get ( "http://bit.ly/1LouEKr" )يوفر Resty سهلة الاستخدام معلمات مسار URL للطلب الديناميكي. يمكن تعيين Params على مستوى العميل ومستوى الطلب. يمكن تجاوز قيمة Params على مستوى العميل عند مستوى الطلب.
// Create a Resty Client
client := resty . New ()
client . R (). SetPathParams ( map [ string ] string {
"userId" : "[email protected]" ,
"subAccountId" : "100002" ,
}).
Get ( "/v1/users/{userId}/{subAccountId}/details" )
// Result:
// Composed URL - /v1/users/[email protected]/100002/details يوفر Resty قدرة الوسيطة على التلاعب بالطلب والاستجابة. إنه أكثر مرونة من نهج رد الاتصال.
// Create a Resty Client
client := resty . New ()
// Registering Request Middleware
client . OnBeforeRequest ( func ( c * resty. Client , req * resty. Request ) error {
// Now you have access to Client and current Request object
// manipulate it as per your need
return nil // if its success otherwise return error
})
// Registering Response Middleware
client . OnAfterResponse ( func ( c * resty. Client , resp * resty. Response ) error {
// Now you have access to Client and current Response object
// manipulate it as per your need
return nil // if its success otherwise return error
})يوفر Resty خطافات onerror التي يمكن استدعاؤها لأن:
إذا كان هناك استجابة من الخادم ، فسيتم لف الخطأ الأصلي في *resty.ResponseError الذي يحتوي على آخر استجابة تم استلامها.
// Create a Resty Client
client := resty . New ()
client . OnError ( func ( req * resty. Request , err error ) {
if v , ok := err .( * resty. ResponseError ); ok {
// v.Response contains the last response from the server
// v.Err contains the original error
}
// Log the error, increment a metric, etc...
})الرجوع: curl_cmd_test.go
// Create a Resty Client
client := resty . New ()
resp , err := client . R ().
SetDebug ( true ).
EnableGenerateCurlOnDebug (). // CURL command generated when debug mode enabled with this option
SetBody ( map [ string ] string { "name" : "Alex" }).
Post ( "https://httpbin.org/post" )
curlCmdExecuted := resp . Request . GenerateCurlCommand ()
// Explore curl command
fmt . Println ( "Curl Command: n " , curlCmdExecuted + " n " )
/* Output
Curl Command:
curl -X POST -H 'Content-Type: application/json' -H 'User-Agent: go-resty/2.14.0 (https://github.com/go-resty/resty)' -d '{"name":"Alex"}' https://httpbin.org/post
*/ يوفر Resty القليل من السياسات (سياسات) إعادة التوجيه أيضًا ، وهو يدعم سياسات متعددة معًا.
// Create a Resty Client
client := resty . New ()
// Assign Client Redirect Policy. Create one as per you need
client . SetRedirectPolicy ( resty . FlexibleRedirectPolicy ( 15 ))
// Wanna multiple policies such as redirect count, domain name check, etc
client . SetRedirectPolicy ( resty . FlexibleRedirectPolicy ( 20 ),
resty . DomainCheckRedirectPolicy ( "host1.com" , "host2.org" , "host3.net" ))قم بتنفيذ واجهة RedirectPolicy وتسجيلها مع عميل Resty. احصل على نظرة redirect.go لمزيد من المعلومات.
// Create a Resty Client
client := resty . New ()
// Using raw func into resty.SetRedirectPolicy
client . SetRedirectPolicy ( resty . RedirectPolicyFunc ( func ( req * http. Request , via [] * http. Request ) error {
// Implement your logic here
// return nil for continue redirect otherwise return error to stop/prevent redirect
return nil
}))
//---------------------------------------------------
// Using struct create more flexible redirect policy
type CustomRedirectPolicy struct {
// variables goes here
}
func ( c * CustomRedirectPolicy ) Apply ( req * http. Request , via [] * http. Request ) error {
// Implement your logic here
// return nil for continue redirect otherwise return error to stop/prevent redirect
return nil
}
// Registering in resty
client . SetRedirectPolicy ( CustomRedirectPolicy { /* initialize variables */ }) // Create a Resty Client
client := resty . New ()
// Custom Root certificates, just supply .pem file.
// you can add one or more root certificates, its get appended
client . SetRootCertificate ( "/path/to/root/pemFile1.pem" )
client . SetRootCertificate ( "/path/to/root/pemFile2.pem" )
// ... and so on!
// Adding Client Certificates, you add one or more certificates
// Sample for creating certificate object
// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.
cert1 , err := tls . LoadX509KeyPair ( "certs/client.pem" , "certs/client.key" )
if err != nil {
log . Fatalf ( "ERROR client certificate: %s" , err )
}
// ...
// You add one or more certificates
client . SetCertificates ( cert1 , cert2 , cert3 ) // Custom Root certificates from string
// You can pass you certificates through env variables as strings
// you can add one or more root certificates, its get appended
client . SetRootCertificateFromString ( "-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----" )
client . SetRootCertificateFromString ( "-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----" )
// ... and so on!
// Adding Client Certificates, you add one or more certificates
// Sample for creating certificate object
// Parsing public/private key pair from a pair of files. The files must contain PEM encoded data.
cert1 , err := tls . X509KeyPair ([] byte ( "-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----" ), [] byte ( "-----BEGIN CERTIFICATE-----content-----END CERTIFICATE-----" ))
if err != nil {
log . Fatalf ( "ERROR client certificate: %s" , err )
}
// ...
// You add one or more certificates
client . SetCertificates ( cert1 , cert2 , cert3 ) تدعم الافتراضي Go الوكيل عبر البيئة المتغير HTTP_PROXY . يوفر Resty الدعم عبر SetProxy و RemoveProxy . اختر حسب حاجتك.
إعدادات وكيل مستوى العميل المطبق على جميع الطلبات
// Create a Resty Client
client := resty . New ()
// Setting a Proxy URL and Port
client . SetProxy ( "http://proxyserver:8888" )
// Want to remove proxy setting
client . RemoveProxy ()يستخدم Resty التراجع لزيادة فترات إعادة المحاولة بعد كل محاولة.
مثال الاستخدام:
// Create a Resty Client
client := resty . New ()
// Retries are configured per client
client .
// Set retry count to non zero to enable retries
SetRetryCount ( 3 ).
// You can override initial retry wait time.
// Default is 100 milliseconds.
SetRetryWaitTime ( 5 * time . Second ).
// MaxWaitTime can be overridden as well.
// Default is 2 seconds.
SetRetryMaxWaitTime ( 20 * time . Second ).
// SetRetryAfter sets callback to calculate wait time between retries.
// Default (nil) implies exponential backoff with jitter
SetRetryAfter ( func ( client * resty. Client , resp * resty. Response ) (time. Duration , error ) {
return 0 , errors . New ( "quota exceeded" )
})افتراضيًا ، ستقوم Resty بإعادة إعادة إعادة طلب إرجاع خطأ غير NIL أثناء التنفيذ. لذلك ، فإن الإعداد أعلاه سيؤدي إلى إعادة إعادة إعادة بناء طلبات مع أخطاء غير NIL تصل إلى 3 مرات ، مع زيادة التأخير بعد كل محاولة.
يمكنك تزويد العميل اختياريًا بظروف إعادة إعادة محاكمة مخصصة:
// Create a Resty Client
client := resty . New ()
client . AddRetryCondition (
// RetryConditionFunc type is for retry condition function
// input: non-nil Response OR request execution error
func ( r * resty. Response , err error ) bool {
return r . StatusCode () == http . StatusTooManyRequests
},
) سيقوم المثال أعلاه بتقديم طلبات إعادة محاولة RERED التي تنتهي برمز حالة 429 Too Many Requests . من المهم أن نلاحظ أنه عند تحديد الشروط باستخدام AddRetryCondition ، فإنه سيتجاوز سلوك إعادة المحاولة الافتراضي ، الذي يحفظ على الأخطاء التي تمت مواجهتها أثناء الطلب. إذا كنت ترغب في إعادة المحاولة على الأخطاء التي تمت مواجهتها أثناء الطلب ، على غرار السلوك الافتراضي ، فستحتاج إلى تكوينه على النحو التالي:
// Create a Resty Client
client := resty . New ()
client . AddRetryCondition (
func ( r * resty. Response , err error ) bool {
// Including "err != nil" emulates the default retry behavior for errors encountered during the request.
return err != nil || r . StatusCode () == http . StatusTooManyRequests
},
)يمكن إضافة شروط إعادة محاولة متعددة. لاحظ أنه إذا تم تحديد شروط متعددة ، فسيحدث إعادة المحاولة إذا تم استيفاء أي من الشروط.
من الممكن أيضًا استخدام resty.Backoff(...) لتنفيذ سيناريوهات إعادة المحاولة التعسفية. مرجع.
// Create a Resty Client
client := resty . New ()
// Allow GET request with Payload. This is disabled by default.
client . SetAllowGetMethodPayload ( true ) // Here you go!
// Client 1
client1 := resty . New ()
client1 . R (). Get ( "http://httpbin.org" )
// ...
// Client 2
client2 := resty . New ()
client2 . R (). Head ( "http://httpbin.org" )
// ...
// Bend it as per your need!!! // Create a Resty Client
client := resty . New ()
// Unique settings at Client level
//--------------------------------
// Enable debug mode
client . SetDebug ( true )
// Assign Client TLSClientConfig
// One can set custom root-certificate. Refer: http://golang.org/pkg/crypto/tls/#example_Dial
client . SetTLSClientConfig ( & tls. Config { RootCAs : roots })
// or One can disable security check (https)
client . SetTLSClientConfig ( & tls. Config { InsecureSkipVerify : true })
// Set client timeout as per your need
client . SetTimeout ( 1 * time . Minute )
// You can override all below settings and options at request level if you want to
//--------------------------------------------------------------------------------
// Host URL for all request. So you can use relative URL in the request
client . SetBaseURL ( "http://httpbin.org" )
// Headers for all request
client . SetHeader ( "Accept" , "application/json" )
client . SetHeaders ( map [ string ] string {
"Content-Type" : "application/json" ,
"User-Agent" : "My custom User Agent String" ,
})
// Cookies for all request
client . SetCookie ( & http. Cookie {
Name : "go-resty" ,
Value : "This is cookie value" ,
Path : "/" ,
Domain : "sample.com" ,
MaxAge : 36000 ,
HttpOnly : true ,
Secure : false ,
})
client . SetCookies ( cookies )
// URL query parameters for all request
client . SetQueryParam ( "user_id" , "00001" )
client . SetQueryParams ( map [ string ] string { // sample of those who use this manner
"api_key" : "api-key-here" ,
"api_secret" : "api-secret" ,
})
client . R (). SetQueryString ( "productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more" )
// Form data for all request. Typically used with POST and PUT
client . SetFormData ( map [ string ] string {
"access_token" : "BC594900-518B-4F7E-AC75-BD37F019E08F" ,
})
// Basic Auth for all request
client . SetBasicAuth ( "myuser" , "mypass" )
// Bearer Auth Token for all request
client . SetAuthToken ( "BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F" )
// Enabling Content length value for all request
client . SetContentLength ( true )
// Registering global Error object structure for JSON/XML request
client . SetError ( & Error {}) // or resty.SetError(Error{}) unixSocket := "/var/run/my_socket.sock"
// Create a Go's http.Transport so we can set it in resty.
transport := http. Transport {
Dial : func ( _ , _ string ) (net. Conn , error ) {
return net . Dial ( "unix" , unixSocket )
},
}
// Create a Resty Client
client := resty . New ()
// Set the previous transport that we created, set the scheme of the communication to the
// socket and set the unixSocket as the HostURL.
client . SetTransport ( & transport ). SetScheme ( "http" ). SetBaseURL ( unixSocket )
// No need to write the host's URL on the request, just the path.
client . R (). Get ( "http://localhost/index.html" )يمكن بناء Resty واختبارها واعتمد عليها عبر Bazel. على سبيل المثال ، لتشغيل جميع الاختبارات:
bazel test :resty_test من أجل السخرية من طلبات HTTP عند اختبار التطبيق الخاص بك ، يمكنك استخدام مكتبة httpmock .
عند استخدام عميل Resty الافتراضي ، يجب عليك تمرير العميل إلى المكتبة على النحو التالي:
// Create a Resty Client
client := resty . New ()
// Get the underlying HTTP Client and set it to Mock
httpmock . ActivateNonDefault ( client . GetClient ())يمكن العثور هنا على مثال أكثر تفصيلاً على طلبات HTTP التي تسخر من Resty باستخدام Ginko.
RESTY تصدر الإصدارات وفقًا للنسخة الدلالية
gopkg.in لإصدار المكتبة.go mod منذ الإصدار v1.10.0 .gopkg.in لتوفير الإصدار. gopkg.in/resty.vX يشير إلى الإصدارات المناسبة الموسومة ؛ يشير X إلى رقم سلسلة الإصدار وهو إصدار مستقر لاستخدام الإنتاج. لـ EG gopkg.in/resty.v0 .أرحب مساهمتك! إذا وجدت أي تحسين أو مشكلة تريد إصلاحها ، فلا تتردد في إرسال طلب سحب ، فأنا أحب طلبات السحب التي تتضمن حالات اختبار لإصلاح/تحسين. لقد بذلت قصارى جهدي لجلب تغطية رمز جيدة. لا تتردد في كتابة الاختبارات.
راجع للشغل ، أود أن أعرف ما هو رأيك في Resty . يرجى فتح مشكلة أو أرسل لي بريدًا إلكترونيًا ؛ هذا يعني الكثير بالنسبة لي.
Jeevanandam M. ([email protected])
إلقاء نظرة على صفحة الأعضاء.
إلقاء نظرة على صفحة المساهمين.
تم إصدار Resty بموجب ترخيص MIT ، راجع ملف الترخيص.