Rspec api documentation + apitome request body in json format

841 Views Asked by At

I am using rspec api documentation, apitome and oauth2 protocol to build acceptance test to our api. Everything works great with one exception that the request body shown as below:

enter image description here

What I noticed from the generated doc above:

Content-Type: application/x-www-form-urlencoded
That should be application/json instead. I have gone through some blog post and suggested to put this:

post "/api/v1/ticket_carts" do
    header "Content-Type", "application/json"

I tried to inspect the params at the server

def create
    render json: params
    # options = set_ticket_cart_protected_params(filter_params)
    # adapter = Adapter::TicketCart.new(options)
    # @handler = adapter.handler
    # @handler.call

    # render json: @handler.order, serializer: adapter.serializer_class
  end

The response looked something like this:

enter image description here

From what I saw the server still got params from the client in form-encoded format.

To my conclusion, rspec api documentation did not send content-type as application/json to the server.

Is there anything I've done wrong or what other step need to take to make rspec api documentation send the right content type ( application/json )?

1

There are 1 best solutions below

0
On BEST ANSWER

I found this in the rspec api doc config

# Change how the post body is formatted by default, you can still override by `raw_post`
  # Can be :json, :xml, or a proc that will be passed the params

then I tried this

# set content type to json
header "Content-Type", "application/json"
#override the params to json format
let(:raw_post) { params.to_json }