Content-Length not set when using BodyJSON #49
-
I have an API that I am making a POST call to with some data like the following code snippet: // "object" is a struct
err := requests.URL("https://example.com/api/something").BodyJSON(object).Fetch(context.Background()) But the actual HTTP POST is missing the Content-Length Header, causing the server to respond with a 400 Bad Request (actually it probably should be 411 Length Required but that's a discussion I've yet to have with those devs). The docs for NewRequestWithContext say the following:
A brief look at the So how can I get the header added with the correct value? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The standard library is inconsistent about when it wants a Reader and when it wants a ReadCloser. I am adapting Readers into ReadClosers with io.NopCloser, but it looks like that is interfering with the Content Length setter. I think the fix is that I should write my own NopCloser type and just strip it off the body passed into NewRequestWithContext, but I’m not sure. I’ll poke around and see what I can figure out. |
Beta Was this translation helpful? Give feedback.
The standard library is inconsistent about when it wants a Reader and when it wants a ReadCloser. I am adapting Readers into ReadClosers with io.NopCloser, but it looks like that is interfering with the Content Length setter. I think the fix is that I should write my own NopCloser type and just strip it off the body passed into NewRequestWithContext, but I’m not sure. I’ll poke around and see what I can figure out.