package utilities import ( "encoding/json" "errors" "io" "net/http" "strings" ) func GetHttpRequestToWordpress(url, user, pass string) []byte { request, err := http.NewRequest("GET", url, nil) addAuth(user, pass, request) CheckError(err) body, _ := requestToWordpress(request, user, pass) return body } func PostHttpRequestToWordpress(url, user, pass string, parameters any) ([]byte, error) { requestBodyBytes, err := json.Marshal(parameters) requestBody := string(requestBodyBytes) if err != nil { return make([]byte, 0), err } request, err := http.NewRequest("POST", url, strings.NewReader(requestBody)) if err != nil { return make([]byte, 0), err } body, err := requestToWordpress(request, user, pass) return body, err } func requestToWordpress(request *http.Request, user, pass string) ([]byte, error) { client := &http.Client{} addAuth(user, pass, request) response, err := client.Do(request) if err != nil { return make([]byte, 0), err } defer CloseBodyAndCheckError(response.Body) err = checkHttpResponseStatus(response) if err != nil { return make([]byte, 0), err } body, err := io.ReadAll(response.Body) CheckError(err) return body, nil } func addAuth(user, pass string, request *http.Request) { request.SetBasicAuth(user, pass) request.Header.Add("content-type", "application/json") } func checkHttpResponseStatus(response *http.Response) error { if response.StatusCode >= 200 && response.StatusCode < 300 { return nil } else { return errors.New("Status Code:" + response.Status + http.StatusText(response.StatusCode)) } }