gpt4 book ai didi

ruby-on-rails - 如何在 rspec 请求规范中设置请求 header ?

转载 作者:IT老高 更新时间:2023-10-28 12:41:52 28 4
gpt4 key购买 nike

在 Controller 规范中,我可以像这样设置 http 接受 header :

request.accept = "application/json"

但在请求规范中,“请求”对象为零。那么我在这里怎么做呢?

我想将http接受 header 设置为json的原因是我可以这样做:

get '/my/path'

而不是这个

get '/my/path.json'

最佳答案

您应该能够将 HTTP header 指定为 get() 方法的第三个参数,如下所述:

http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get

这里

http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-process

所以,你可以试试这样的:

get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}

关于ruby-on-rails - 如何在 rspec 请求规范中设置请求 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654465/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com