json_api_client icon indicating copy to clipboard operation
json_api_client copied to clipboard

HTTP requests have the wrong Content-Type header

Open doga opened this issue 9 years ago • 3 comments

Hello,

I have noticed that JsonApiClient::Resource sends HTTP requests with a "Content-Type: application/json" header. Here is what needs fixing:

This is a library that I plan on using in production if I can, so congrats.

doga avatar Feb 19 '16 16:02 doga

I have a similar problem when accessing an endpoint implemented in another language where the developer had to roll their own implementation, and made some mistakes. I need to add a media header to all GET requests, and the content type has to be the standard media type for JSON API.

But I can't find where I make the change. I suspect it's in the middleware.

JohnSmall avatar Jul 27 '16 15:07 JohnSmall

@JohnSmall You are correct in your assumption that it requires you to add it in the middleware. I ran into a similar issue and was able to override the headers by adding my own middleware class like so:

module Api
    class Request < Faraday::Middleware
      def call(environment)
        environment[:request_headers]["Content-Type"]   = "application/vnd.api+json,application/vnd.api+json;com.api.version=#{Base.version}"
        environment[:request_headers]["Accept"]         = "application/vnd.api+json,application/vnd.api+json;com.api.version=#{Base.version}"
        environment[:request_headers]["User-Agent"]     = "Api Ruby Client v0.1.0"
        environment[:request_headers]["Authorization"]  = "Token token=#{Base.access_token}"
        @app.call(environment)
      end
    end
end

As per the example in the README

natemacinnes avatar Aug 22 '16 17:08 natemacinnes

Thanks for the advice

Any idea how I would modify the middleware to deal with SSL client certificates?

JohnSmall avatar Oct 06 '16 10:10 JohnSmall