Request
发送请求
Request
的以下这些方法会发起 HTTP 请求并返回响应。
MustXXX
这种方法不会返回任何错误,如果发生错误就会 panic,适合测试 API 的时候用。
- Get(url string)
- Head(url string)
- Post(url string)
- Delete(url string)
- Patch(url string)
- Options(url string)
- Put(url string)
- MustGet(url string)
- MustHead(url string)
- MustPost(url string)
- MustDelete(url string)
- MustPatch(url string)
- MustOptions(url string)
- MustPut(url string)
- Send(method, url string) - 使用指定的 Method 和 URL 发送请求。
- Do(ctx …context.Context) - 传入可选的 context 并发送请求。
Request 设置
以下是 Request
设置相关的方法,它们都有对应的全局包装方法(测试时不需要显式创建 Client
和 Request
,直接调用全局同名方法),基本上可以从方法命名就能直接看出设置的含义。
URL 查询参数和路径参数
- AddQueryParam(key, value string)
- SetQueryParam(key, value string)
- SetQueryParams(params map[string]string)
- SetQueryParamsAnyType(params map[string]interface{})
- SetQueryString(query string)
- SetPathParam(key, value string)
- SetPathParams(params map[string]string)
Header 和 Cookie
- SetHeader(key, value string)
- SetHeaders(hdrs map[string]string)
- SetHeaderOrder(keys …string)
- SetPseudoHeaderOrder(keys …string)
- SetBasicAuth(username, password string)
- SetBearerAuthToken(token string)
- SetDigestAuth(username, password string)
- SetContentType(contentType string)
- SetCookies(cookies …*http.Cookie)
Body 和 Marshal&Unmarshal
- SetBody(body interface{})
- SetBodyBytes(body []byte)
- SetBodyJsonBytes(body []byte)
- SetBodyJsonMarshal(v interface{})
- SetBodyJsonString(body string)
- SetBodyString(body string)
- SetBodyXmlBytes(body []byte)
- SetBodyXmlMarshal(v interface{})
- SetBodyXmlString(body string)
- SetSuccessResult(result interface{})
- SetErrorResult(error interface{})
请求级别的 Debug
- EnableTrace() - trace 默认是关闭的
- DisableTrace()
- EnableDump()
- EnableDumpTo(output io.Writer)
- EnableDumpToFile(filename string)
- EnableDumpWithoutBody()
- EnableDumpWithoutHeader()
- EnableDumpWithoutRequest()
- EnableDumpWithoutRequestBody()
- EnableDumpWithoutResponse()
- EnableDumpWithoutResponseBody()
- SetDumpOptions(opt *DumpOptions)
Multipart & 表单 & 上传
- SetFormData(data map[string]string)
- SetFormDataAnyType(data map[string]interface{}) - SetFormData 的语法糖
- SetFormDataFromValues(data url.Values)
- SetFile(paramName, filePath string)
- SetFiles(files map[string]string)
- SetFileBytes(paramName, filename string, content []byte)
- SetFileReader(paramName, filePath string, reader io.Reader)
- SetFileUpload(uploads …FileUpload) - 完全自定义上传选项
- SetUploadCallback(callback UploadCallback)
- SetUploadCallbackWithInterval(callback UploadCallback, minInterval time.Duration)
下载
- SetOutput(output io.Writer)
- SetOutputFile(file string)
- SetDownloadCallback(callback DownloadCallback)
- SetDownloadCallbackWithInterval(callback DownloadCallback, minInterval time.Duration)
自动重试
- SetRetryCount(count int)
- SetRetryInterval(getRetryIntervalFunc GetRetryIntervalFunc)
- SetRetryFixedInterval(interval time.Duration)
- SetRetryBackoffInterval(min, max time.Duration)
- SetRetryHook(hook RetryHookFunc)
- AddRetryHook(hook RetryHookFunc)
- SetRetryCondition(condition RetryConditionFunc)
- AddRetryCondition(condition RetryConditionFunc)