Overriding the default Content-Type

axios default content-type
content-type header react
how to set multiple content-type in javascript
axios content-type text csv
content-type: application/json
headers set content-type': 'application/json
content-type application/json not working
javascript set content-type: application/json

I have a Rails API which accepts only JSON as input. If I fail to include a header of Content-Type: application/json, then request.headers['Content-Type'] defaults to application/x-www-form-urlencoded and the params do not get parsed properly. The whole json body becomes a key in the params. The result is a 422, which is confusing to API users.

How can I change this to default to parsing as json if no Content-Type header is supplied?

Lots of other questions answer how to do this with the response format. To change this default, you can specify it in the controller with:

 request.format = :json

Or in a route namespace with something like:

 namespace :api, defaults: {format: :json} do

This, however, changes the default response format and does not change the default request format. What I need to do is to change the default request format for parsing parameters.


parsed = JSON.parse(json_body) unless request.headers["Content-Type"] == 'application/json'

Default methods Content-Type headers cant be overridden · Issue , Unable to override default content-type 'application/json' header #988. Closed. aleksdao opened this issue on Aug 14, 2017 · 1 comment. Closed  Default content type will be applied while drag and drop. Source: Drag and Drop document in a library with multiple content types associated If drag and drop is mainly causing your problems, you could disable the whole function.


I've solved it with middleware this way for Rails API (rails new my_project --api)

config:

# config/application.rb
# ...
require './lib/middleware/consider_all_request_json_middleware'
# ...

module MyApplication
    # ...
  class Application < Rails::Application
        # ...
        config.middleware.insert_before(ActionDispatch::Static,ConsiderAllRequestJsonMiddleware)
        # ...

middleware:

# lib/middleware/consider_all_request_json_middleware.rb
class ConsiderAllRequestJsonMiddleware
  def initialize app
    @app = app
  end

  def call(env)
    if env["CONTENT_TYPE"] == 'application/x-www-form-urlencoded'
      env["CONTENT_TYPE"] = 'application/json'
    end
    @app.call(env)
  end
end

original: https://blog.eq8.eu/til/content-type-applicationjson-by-default-in-rails-5.html

Unable to override default content-type 'application/json' header , There's really no simple solution for this one. If you are fine with defining the content type based on e.g. file extension, you could add an event  For example, you can override the product content type template to change its layout. Applying a modified content type template You can apply a modified content type template to override a default content type template in the Developer Portal .


Here is my admittedly terrible solution derived from the suggestion in Micael Nussbaumer's answer. I'd love it if some Rubyists could magically turn this ugly hack into a pithy one liner.

module Api
  class BaseApiController < ActionController::API

  private
    # This is an ugly hack needed to make it default to json if you do not
    # specify a Content-Type.  If you see this and know of a better way please
    # say so!
    def params
      if !@params
        if request.headers["Content-Type"]=="application/x-www-form-urlencoded"
          body_string = request.body.read
          begin
            hash = JSON.parse(body_string)
            @params = ActionController::Parameters.new(hash)
          rescue
            # do nothing
          end
        end
        if !@params
          @params = super
        end
      end
      @params
    end

    ...

end

Override default content type, DEFAULT: If contentType is not present in the Message header or the binding, a per-binding basis, which essentially lets you override the default content type. Applying a modified content type template. You can apply a modified content type template to override a default content type template in the Developer Portal. You must have administrator access to complete this task. You must also obtain a custom theme and a copy of the existing template.


8. Content Type Negotiation, The content-type: image/gif indicates that image and subtype is gif image. How to find out default mine types in Nginx. Look for config file named  We chatted briefly yesterday about the issue I was having with overriding the default content-type application/json header that is set by Apollo Client. If you have any thoughts on the best way to resolve this, let me know! I'm happy to contribute back a PR to extend the client's flexibility. Intended outcome:


How to override content type with Nginx web server, Hi, I'd like to understand more about how REST-assured (or the underlying libraries) decide what Content-Type to send for a given request. Overriding or changing default MIME types for artifact downloads By default when Nexus downloads files into proxy repositories it validates that the downloaded file's contents match what is expected based on the file's MIME type.


default Content-Type and overriding it, Gets or sets the value of the Content-type HTTP header. public override string ContentType { get; set; } member this.ContentType The default value is null . The Content-Type entity header is used to indicate the media type of the resource. In responses, a Content-Type header tells the client what the content type of the returned content actually is. Browsers will do MIME sniffing in some cases and will not necessarily follow the value of this header; to prevent this behavior, the header X-Content-Type-Options can be set to nosniff .