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

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/url"
     8  )
     9  
    10  // Put issues a PUT to the specified URL.
    11  //
    12  // Caller should close resp.Body when done reading from it.
    13  //
    14  // If the provided body is an io.Closer, it is closed after the
    15  // request.
    16  //
    17  // To set custom headers, use NewRequest and DefaultClient.Do.
    18  //
    19  // See the Client.Do method documentation for details on how redirects
    20  // are handled.
    21  func Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) {
    22  	req, err := http.NewRequestWithContext(ctx, "PUT", url, body)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	req.Header.Set("Content-Type", contentType)
    27  	return http.DefaultClient.Do(req)
    28  }
    29  
    30  func RemoveQueryParam(qurl string) string {
    31  	u, err := url.Parse(qurl)
    32  	if err != nil {
    33  		return qurl
    34  	}
    35  	u.RawQuery = ""
    36  	return u.String()
    37  }