Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content type should be case insensitive #680

Closed
greg-freewave opened this issue Jun 14, 2024 · 2 comments · Fixed by #685
Closed

Content type should be case insensitive #680

greg-freewave opened this issue Jun 14, 2024 · 2 comments · Fixed by #685

Comments

@greg-freewave
Copy link

When consuming a json api using tesla, the request body wasn't being correctly decoded as JSON. This is because the server was returning a content type of application/JSON but tesla expects content type be all lower case:
https://github.com/elixir-tesla/tesla/blob/master/lib/tesla/middleware/json.ex#L48

According to the rfc https://datatracker.ietf.org/doc/html/rfc2045#section-2

All media type values, subtype values, and parameter names as defined are case-insensitive.

@yordis
Copy link
Member

yordis commented Jun 14, 2024

Could you share an example call for me to test manually? I would like to check the headers myself

@greg-freewave
Copy link
Author

greg-freewave commented Jun 16, 2024

Just using this http mocking website, you can see the returned content type is application/JSON:

curl -v -X GET 'https://testlajson.free.beeceptor.com/todos' -v

I have no idea how long that link will run, I don't have an account at that website i just found it with a google search.

application/JSON body is a string:

Tesla.get(Tesla.client([Tesla.Middleware.JSON]), "https://testlajson.free.beeceptor.com/todos")
{:ok,
 %Tesla.Env{
   method: :get,
   url: "https://testlajson.free.beeceptor.com/todos",
   query: [],
   headers: [
     {"date", "Sun, 16 Jun 2024 00:19:32 GMT"},
     {"vary", "Accept-Encoding"},
     {"content-length", "577"},
     {"content-type", "application/JSON"},
     {"access-control-allow-origin", "*"},
     {"alt-svc", "h3=\":443\"; ma=2592000"},
     {"x-beeceptor-rule-id", "defaultget"}
   ],
   body: "[\n  {\n    \"id\": 1,\n    \"title\": \"Create an endpoint at Beeceptor.\",\n    \"completed\": true\n  },\n  {\n    \"id\": 2,\n    \"title\": \"Send a request to your new Beeceptor endpoint.\",\n    \"completed\": true\n  },\n  {\n    \"id\": 3,\n    \"title\": \"Create a mocking rule to send dummy data.\",\n    \"completed\": false\n  },\n  {\n    \"id\": 4,\n    \"title\": \"Try out a failure response by sending 500 HTTP status code.\",\n    \"completed\": false\n  },\n  {\n    \"id\": 5,\n    \"title\": \"Use the proxy feature to send a request to a real API. Intercept traffic using Beeceptor.\",\n    \"completed\": false\n  }\n]",
   status: 200,
   opts: [],
   __module__: Tesla,
   __client__: %Tesla.Client{
     fun: nil,
     pre: [{Tesla.Middleware.JSON, :call, [[]]}],
     post: [],
     adapter: nil
   }
 }}

application/json body is a list of maps:

> Tesla.get(Tesla.client([Tesla.Middleware.JSON]), "https://testlajson.free.beeceptor.com/todos")
{:ok,
 %Tesla.Env{
   method: :get,
   url: "https://testlajson.free.beeceptor.com/todos",
   query: [],
   headers: [
     {"date", "Sun, 16 Jun 2024 00:18:49 GMT"},
     {"vary", "Accept-Encoding"},
     {"content-length", "577"},
     {"content-type", "application/json"},
     {"access-control-allow-origin", "*"},
     {"alt-svc", "h3=\":443\"; ma=2592000"},
     {"x-beeceptor-rule-id", "defaultget"}
   ],
   body: [
     %{
       "completed" => true,
       "id" => 1,
       "title" => "Create an endpoint at Beeceptor."
     },
     %{
       "completed" => true,
       "id" => 2,
       "title" => "Send a request to your new Beeceptor endpoint."
     },
     %{
       "completed" => false,
       "id" => 3,
       "title" => "Create a mocking rule to send dummy data."
     },
     %{
       "completed" => false,
       "id" => 4,
       "title" => "Try out a failure response by sending 500 HTTP status code."
     },
     %{
       "completed" => false,
       "id" => 5,
       "title" => "Use the proxy feature to send a request to a real API. Intercept traffic using Beeceptor."
     }
   ],
   status: 200,
   opts: [],
   __module__: Tesla,
   __client__: %Tesla.Client{
     fun: nil,
     pre: [{Tesla.Middleware.JSON, :call, [[]]}],
     post: [],
     adapter: nil
   }
 }}

@greg-freewave greg-freewave changed the title Content types should be case sensitive Content types should be case insensitive Jun 16, 2024
@greg-freewave greg-freewave changed the title Content types should be case insensitive Content type should be case insensitive Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants