HTTPPostContext post 请求
(ctx context.Context, uri string, data []byte, header map[string]string)
| 63 | |
| 64 | // HTTPPostContext post 请求 |
| 65 | func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[string]string) ([]byte, error) { |
| 66 | if uriModifier != nil { |
| 67 | uri = uriModifier(uri) |
| 68 | } |
| 69 | body := bytes.NewBuffer(data) |
| 70 | request, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, body) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | for key, value := range header { |
| 76 | request.Header.Set(key, value) |
| 77 | } |
| 78 | |
| 79 | response, err := DefaultHTTPClient.Do(request) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | |
| 84 | defer response.Body.Close() |
| 85 | if response.StatusCode != http.StatusOK { |
| 86 | return nil, fmt.Errorf("http post error : uri=%v , statusCode=%v", uri, response.StatusCode) |
| 87 | } |
| 88 | return io.ReadAll(response.Body) |
| 89 | } |
| 90 | |
| 91 | // PostJSONContext post json 数据请求 |
| 92 | func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte, error) { |
no test coverage detected
searching dependent graphs…