This feels idiotic, but I'm chasing my tail here what the difference is between a cURL I'm using, which hits an endpoint properly and returns a 200 response, and what the golang native http
library is doing. I need my Golang to send an HTTP POST exactly like my cURL and feel like I'm missing a magic word.
The cURL that does work is exactly like this (actual url and -F
values are spoofed, obviously):
curl -v -X POST https://my-endpoint.com/jwt \
-H 'Content-Type: multipart/form-data' \
-F 'client_id=client123' \
-F 'client_secret=secret456' \
-F 'jwt_token=redacted.generated.jwtString'
Unfortunately, I must send it as multipart/form-data
- despite it just being basic key:value strings.
Now, in the Golang , the form data is given to me as map[string]interface{}
. I'm attempting to make the request as such:
urlStr := "https://my-endpoint.com/jwt"
bodyMap := make(map[string]interface{})
bodyMap["client_id"] = "client123"
bodyMap["client_secret"] = "secret456"
bodyMap["jwt_token"] = "redacted.generated.jwtString"
jsonBodyBytes, err := json.Marshal(bodyMap)
if err != nil {
return fmt.Errorf("marshalling access token request body, err:%w", err)
}
req, err := http.NewRequest(http.MethodPost, urlStr, bytes.NewReader(jsonBodyBytes))
if err != nil {
return fmt.Errorf("constructing access token request, url=%s, err=%w", s.TokenEndpointURL, err)
}
req.Header.Set("Content-Type", "multipart/form-data")
fmt.Printf("request: %+v", req)
In real life, the actual values of the body are longer, but one thing that's a big difference I notice is the content-length
difference between what appears in the cURL
output vs what appears in the fmt.Printf
above. The Golang one is ~810 and the cURL one is something like ~1100 . So this has a smell to me that I'm not setting something properly in the Golang with the body encoding.
Any ideas here? thank you!