func prepareRequest(postBody interface{}, headerParams map[string]string, queryParams url.Values, formParams map[string]string, fileName string, fileBytes []byte) *resty.Request { request := resty.R() request.SetBody(postBody) // add header parameter, if any if len(headerParams) > 0 { request.SetHeaders(headerParams) } // add query parameter, if any if len(queryParams) > 0 { request.SetMultiValueQueryParams(queryParams) } // add form parameter, if any if len(formParams) > 0 { request.SetFormData(formParams) } if len(fileBytes) > 0 && fileName != "" { _, fileNm := filepath.Split(fileName) request.SetFileReader("file", fileNm, bytes.NewReader(fileBytes)) } return request }
func Example_get() { resp, err := resty.R().Get("http://httpbin.org/get") fmt.Printf("\nError: %v", err) fmt.Printf("\nResponse Status Code: %v", resp.StatusCode()) fmt.Printf("\nResponse Status: %v", resp.Status()) fmt.Printf("\nResponse Body: %v", resp) fmt.Printf("\nResponse Time: %v", resp.Time()) fmt.Printf("\nResponse Recevied At: %v", resp.ReceivedAt) }
func Example_enhancedGet() { resp, err := resty.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") printOutput(resp, err) }
func Example_put() { // Just one sample of PUT, refer POST for more combination // request goes as JSON content type // No need to set auth token, error, if you have client level settings resp, err := resty.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") printOutput(resp, err) }
func Example_dropboxUpload() { // For example: upload file to Dropbox // POST of raw bytes for file upload. file, _ := os.Open("/Users/jeeva/mydocument.pdf") fileBytes, _ := ioutil.ReadAll(file) // See we are not setting content-type header, since go-resty automatically detects Content-Type for you resp, err := resty.R(). SetBody(fileBytes). // resty autodetects content type SetContentLength(true). // Dropbox expects this value SetAuthToken("<your-auth-token>"). SetError(DropboxError{}). Post("https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf") // you can use PUT method too dropbox supports it // Output print fmt.Printf("\nError: %v\n", err) fmt.Printf("Time: %v\n", resp.Time()) fmt.Printf("Body: %v\n", resp) }
func CreatePuzzle(puzzler_host string, username string, tiles []*chopper.TileEntry) (int, []byte, error) { pieces, _ := json.Marshal(tiles) auth_and_host := strings.Split(puzzler_host, "@") println(auth_and_host[0], auth_and_host[1]) auth := strings.Split(auth_and_host[0], ":") resp, err := resty.R(). SetBasicAuth(auth[0], auth[1]). SetFormData(map[string]string{ "username": username, "pieces": string(pieces), }). SetHeader("Content-Type", "application/json"). Post("http://" + auth_and_host[1] + "/puzzles/") if err != nil { return 0, nil, err } return resp.StatusCode(), resp.Body(), err }
func Example_post() { // POST JSON string // No need to set content type, if you have client level setting resp, err := resty.R(). SetHeader("Content-Type", "application/json"). SetBody(`{"username":"******", "password":"******"}`). SetResult(AuthSuccess{}). // or SetResult(&AuthSuccess{}). Post("https://myapp.com/login") printOutput(resp, err) // POST []byte array // No need to set content type, if you have client level setting resp1, err1 := resty.R(). SetHeader("Content-Type", "application/json"). SetBody([]byte(`{"username":"******", "password":"******"}`)). SetResult(AuthSuccess{}). // or SetResult(&AuthSuccess{}). Post("https://myapp.com/login") printOutput(resp1, err1) // POST Struct, default is JSON content type. No need to set one resp2, err2 := resty.R(). SetBody(resty.User{Username: "******", Password: "******"}). SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). SetError(&AuthError{}). // or SetError(AuthError{}). Post("https://myapp.com/login") printOutput(resp2, err2) // POST Map, default is JSON content type. No need to set one resp3, err3 := resty.R(). SetBody(map[string]interface{}{"username": "******", "password": "******"}). SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). SetError(&AuthError{}). // or SetError(AuthError{}). Post("https://myapp.com/login") printOutput(resp3, err3) }