Sha256: d015eb5db4430719cdfa1b058883dc25d3607953ba3aef77b84aeff15bc3eb57

Contents?: true

Size: 1.65 KB

Versions: 24

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'json'

module Faraday
  class Request
    # Request middleware that encodes the body as JSON.
    #
    # Processes only requests with matching Content-type or those without a type.
    # If a request doesn't have a type but has a body, it sets the Content-type
    # to JSON MIME-type.
    #
    # Doesn't try to encode bodies that already are in string form.
    class Json < Middleware
      MIME_TYPE = 'application/json'
      MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze

      def on_request(env)
        match_content_type(env) do |data|
          env[:body] = encode(data)
        end
      end

      private

      def encode(data)
        ::JSON.generate(data)
      end

      def match_content_type(env)
        return unless process_request?(env)

        env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
        yield env[:body] unless env[:body].respond_to?(:to_str)
      end

      def process_request?(env)
        type = request_type(env)
        body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
      end

      def body?(env)
        body = env[:body]
        case body
        when true, false
          true
        when nil
          # NOTE: nil can be converted to `"null"`, but this middleware doesn't process `nil` for the compatibility.
          false
        else
          !(body.respond_to?(:to_str) && body.empty?)
        end
      end

      def request_type(env)
        type = env[:request_headers][CONTENT_TYPE].to_s
        type = type.split(';', 2).first if type.index(';')
        type
      end
    end
  end
end

Faraday::Request.register_middleware(json: Faraday::Request::Json)

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
faraday-2.7.8 lib/faraday/request/json.rb
faraday-2.7.7 lib/faraday/request/json.rb
faraday-2.7.6 lib/faraday/request/json.rb
faraday-2.7.5 lib/faraday/request/json.rb