(url string, body io.Reader, contentType string, xrayErrorCause []byte)
| 127 | } |
| 128 | |
| 129 | func (c *runtimeAPIClient) post(url string, body io.Reader, contentType string, xrayErrorCause []byte) error { |
| 130 | b := newErrorCapturingReader(body) |
| 131 | req, err := http.NewRequest(http.MethodPost, url, b) |
| 132 | if err != nil { |
| 133 | return fmt.Errorf("failed to construct POST request to %s: %v", url, err) |
| 134 | } |
| 135 | req.Trailer = b.Trailer |
| 136 | req.Header.Set("User-Agent", c.userAgent) |
| 137 | req.Header.Set("Content-Type", contentType) |
| 138 | |
| 139 | if xrayErrorCause != nil && len(xrayErrorCause) < xrayErrorCauseMaxSize { |
| 140 | req.Header.Set(headerXRayErrorCause, string(xrayErrorCause)) |
| 141 | } |
| 142 | |
| 143 | resp, err := c.httpClient.Do(req) |
| 144 | if err != nil { |
| 145 | return fmt.Errorf("failed to POST to %s: %v", url, err) |
| 146 | } |
| 147 | defer func() { |
| 148 | if err := resp.Body.Close(); err != nil { |
| 149 | log.Printf("runtime API client failed to close %s response body: %v", url, err) |
| 150 | } |
| 151 | }() |
| 152 | if resp.StatusCode != http.StatusAccepted { |
| 153 | return fmt.Errorf("failed to POST to %s: got unexpected status code: %d", url, resp.StatusCode) |
| 154 | } |
| 155 | |
| 156 | _, err = io.Copy(ioutil.Discard, resp.Body) |
| 157 | if err != nil { |
| 158 | return fmt.Errorf("something went wrong reading the POST response from %s: %v", url, err) |
| 159 | } |
| 160 | |
| 161 | return nil |
| 162 | } |
| 163 | |
| 164 | func newErrorCapturingReader(r io.Reader) *errorCapturingReader { |
| 165 | trailer := http.Header{ |
no test coverage detected