fix(json): do not override custom content-type request header#848
fix(json): do not override custom content-type request header#848arcanemachine wants to merge 1 commit intoelixir-tesla:masterfrom
Conversation
|
I think the "Semantic PR Title" check failed... I believe my PR title is in line with the requirements. Error message:
|
| env | ||
| |> Tesla.put_body(body) | ||
| |> Tesla.put_headers([{"content-type", encode_content_type(opts)}])} | ||
| env_has_content_type_header = |
There was a problem hiding this comment.
is the downcase actually required? under what conditions (adapter specific I am guessing)?
There was a problem hiding this comment.
Re: Downcase - I figured that would be best in the event that some misconfigured server demands Content-Type or similar instead of complying with the (case-insensitive) spec.
Re: Use Tesla.get_header - I was not aware of that function. To resolve the issue an idiomatic manner, I have force-pushed a change to my branch to use that function instead.
The reason I did that extra downcasing work in my initial commit was to accommodate a future situation where poorly-written server demands e.g. a case-sensitive Content-Type header instead of complying with the spec. For my needs, though, Tesla.get_header/2 works fine.
46f0fd1 to
a444a98
Compare
| test "does not put own content-type header if one is already present and body is encodable" do | ||
| env = %Tesla.Env{body: %{}, headers: [{"content-type", "application/x-ndjson"}]} | ||
|
|
||
| {:ok, %Tesla.Env{headers: headers}} = Tesla.Middleware.JSON.encode(env, []) |
There was a problem hiding this comment.
Should this be something explicit as part of the middleware?
{Tesla.Middleware.JSON, content_type: "application/x-ndjson"}a444a98 to
98b99f3
Compare
|
I just realized I had a total XY problem issue. I was having issues with the headers, but my solution fixed the problem for the wrong reason. The JSON middleware already exposes options to set the request headers, which weren't working because my payload was technically a binary (NDJSON is a list of JSON objects, so not "encodable" per JSON middleware). Long story short: My PR is not needed, I was able to set the headers correctly, and was getting errors from another endpoint which was misconfigured and causing errors for an unrelated reason, which I thought was related to the same issue, but was actually something else. Sorry for wasting your time. 😅 |
Preamble
I am working with Elasticsearch, which uses a bulk endpoint that uses NDJSON, and expects a
content-type: application/x-ndjsonheader.However, Tesla's JSON encoder middleware does not allow the header to be overridden, so I have to use workarounds to set the header when sending the request:
Solution
This pull request resolves the issue by allowing the
content-typeheader to be overridden, but falls back to the defaultapplication/jsonif no customcontent-typeheader is present in the request.This allows me to remove my workaround logic and just use plain old
Tesla.Middleware.JSONin my client with the custom header that is required by Elasticsearch.