| 37 | } |
| 38 | |
| 39 | func (c HttpClient) SendRequest(requestBody []byte, requestMethod string, requestUrl string) (string, error) { |
| 40 | say.Info(requestMethod + " " + requestUrl + " " + string(requestBody)) |
| 41 | |
| 42 | responseBody := bytes.NewBuffer(requestBody) |
| 43 | request, requestCreationError := http.NewRequest(requestMethod, requestUrl, responseBody) |
| 44 | |
| 45 | if requestCreationError != nil { |
| 46 | return "", fmt.Errorf("failed to create the http request object: %w", requestCreationError) |
| 47 | } |
| 48 | |
| 49 | request.Header.Set("Content-Type", "application/json") |
| 50 | response, responseErr := c.netHttpClient.Do(request) |
| 51 | if e, ok := responseErr.(*url.Error); ok { |
| 52 | switch e.Err.(type) { |
| 53 | case x509.UnknownAuthorityError: |
| 54 | say.Error("The timer.mob.sh SSL certificate is signed by an unknown authority!") |
| 55 | say.Fix("HINT: You can ignore that by adding MOB_TIMER_INSECURE=true to your configuration or environment.", |
| 56 | "echo MOB_TIMER_INSECURE=true >> ~/.mob") |
| 57 | return "", fmt.Errorf("failed, to make the http request: %w", responseErr) |
| 58 | |
| 59 | default: |
| 60 | return "", fmt.Errorf("failed to make the http request: %w", responseErr) |
| 61 | |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if responseErr != nil { |
| 66 | return "", fmt.Errorf("failed to make the http request: %w", responseErr) |
| 67 | } |
| 68 | if response.StatusCode >= 300 { |
| 69 | return "", errors.New("got an error from the server: " + requestUrl + " " + response.Status) |
| 70 | } |
| 71 | defer response.Body.Close() |
| 72 | bodyBytes, responseReadingErr := ioutil.ReadAll(response.Body) |
| 73 | body := string(bodyBytes) |
| 74 | if responseReadingErr != nil { |
| 75 | return "", fmt.Errorf("failed to read the http response: %w", responseReadingErr) |
| 76 | } |
| 77 | if string(body) != "" { |
| 78 | say.Info(body) |
| 79 | } |
| 80 | return body, nil |
| 81 | } |