package utilities import ( "encoding/json" "fmt" "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 { requestBodyBytes, err := json.Marshal(parameters) requestBody := string(requestBodyBytes) CheckError(err) request, err := http.NewRequest("POST", url, strings.NewReader(requestBody)) CheckError(err) body := requestToWordpress(request, user, pass) return body } func requestToWordpress(request *http.Request, user, pass string) []byte { client := &http.Client{} addAuth(user, pass, request) response, err := client.Do(request) CheckError(err) defer CloseBodyAndCheckError(response.Body) checkHttpResponseStatus(response) body, err := io.ReadAll(response.Body) CheckError(err) return body } func addAuth(user, pass string, request *http.Request) { request.SetBasicAuth(user, pass) request.Header.Add("content-type", "application/json") } func checkHttpResponseStatus(response *http.Response) { if response.StatusCode != 200 { fmt.Println("Status Code:", response.Status, http.StatusText(response.StatusCode)) } }