github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/pkg/http/post.go (about)

     1  package http
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"net/url"
     8  )
     9  
    10  // PostForValues issues a POST to the specified URL and returns the response body as url.Values.
    11  func PostForValues(_url, contentType string, body io.Reader) (url.Values, error) {
    12  	resp, err := http.Post(_url, contentType, body)
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  	defer resp.Body.Close()
    17  	bytes, err := io.ReadAll(resp.Body)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	// FIXME: on error, the body might not be URL-encoded
    22  	values, err := url.ParseQuery(string(bytes))
    23  	if err != nil {
    24  		return nil, fmt.Errorf("failed to parse response body %s: %w", resp.Status, err)
    25  	}
    26  	return values, nil
    27  }