1

我正在使用 rspec api 文档、apime 和 oauth2 协议来为我们的 api 构建验收测试。一切都很好,除了请求正文如下所示:

在此处输入图像描述

我从上面生成的文档中注意到:

Content-Type: application/x-www-form-urlencoded
那应该是 application/json 。我浏览了一些博客文章并建议将其放入:

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

我试图检查服务器上的参数

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

响应看起来像这样:

在此处输入图像描述

从我看到的服务器仍然以表单编码格式从客户端获取参数。

我的结论是,rspec api 文档没有将 content-type 作为 application/json 发送到服务器。

有什么我做错了或者需要采取什么其他步骤来使 rspec api 文档发送正确的内容类型( application/json )?

4

1 回答 1

1

我在 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

然后我尝试了这个

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

于 2018-02-06T06:57:01.737 回答