0

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!

NateH06
  • 3,154
  • 7
  • 32
  • 56
  • 4
    You are not creating a proper multipart/form-data were each part has a key=value. Instead you do some JSON marshaling for some unknown reason. To see how to use multipart/form-data with golang see [POST data using the Content-Type multipart/form-data](https://stackoverflow.com/questions/20205796/post-data-using-the-content-type-multipart-form-data) – Steffen Ullrich Mar 08 '23 at 19:50
  • Notice that the Go snippet is missing a closing double quote on the first line, messing up the syntax highlighting. – Benjamin W. Mar 08 '23 at 20:27
  • @SteffenUllrich that question got me A LOT closer, thank you, particularly [this answer](https://stackoverflow.com/a/67865385/7082628) - but the content length is still off by just a few, only about 4 this time - and is still causing me similar issues. – NateH06 Mar 08 '23 at 20:38
  • multipart messages use boundary markers which can differ in size between implementations and invocations. So you cannot rely on different implementations or invocations resulting in the same content length. *"and is still causing me similar issues."* - no idea what these issues are. – Steffen Ullrich Mar 08 '23 at 20:46
  • Oop, I found an error in my version of that, going to write an answer for my future self below. – NateH06 Mar 08 '23 at 20:51

0 Answers0