JSON 使用 HTTPPARTy 的响应总是 returns 200
JSON response using HTTPPARTy always returns 200
我正在通过 HttpParty 对象向我的 rails API 发送请求:
HTTParty.get(task_list_url(@hotel_id),
:headers => { 'X-User-Email' => @user_email, 'X-User-Token'=> @user_token, 'Content-Type' => 'application/json' }
)
我将此对象存储在@response 变量中
API 上的方法看起来像那样
def index
@tasks = policy_scope(Task).where(hotel: @hotel)
if current_user.created_account == @hotel.account || current_user.hotels.include?(@hotel)
render json: {tasks: @tasks, hotel: @hotel}
else
render json: {
:status => :unauthorized,
:message => "unauthorized"
}
end
end
问题出在其他情况下,@response returned 看起来像这样:
#<HTTParty::Response:0x7fbb0a580198 parsed_response={"status"=>"unauthorized", "message"=>"unauthorized"}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "x-content-type-options"=>["nosniff"], "access-control-allow-origin"=>["*"], "access-control-allow-methods"=>["POST, GET, PUT, DELETE, OPTIONS"], "access-control-allow-headers"=>["Origin, Content-Type, Accept, Authorization, Token"], "access-control-max-age"=>["1728000"], "content-type"=>["application/json; charset=utf-8"], "etag"=>["W/\"2cd59ce00f09c29553693183e9a04a4d\""], "cache-control"=>["max-age=0, private, must-revalidate"], "x-request-id"=>["5acc353c-48aa-410d-a4bc-888c653cae02"], "x-runtime"=>["0.459027"], "vary"=>["Origin"], "connection"=>["close"], "transfer-encoding"=>["chunked"]}>
相关信息不在@response中,如您所见,而是在parsed_response
中。这在我的客户端应用程序中产生了一个问题,因为我正在检查 @response returns OK 200 而正确的 return 应该是 UNAUTHORIZED 401.
如何将正确的 json 响应放入 @response 对象中,或者如果不可能,如何检查 HTTParty 对象中的 parsed_response。
控制器的这个小修正应该可以解决问题。
而不是:
render json: {
:status => :unauthorized,
:message => "unauthorized"
}
使用这个:
render json: { message: "unauthorized" }, status: :unauthorized
我正在通过 HttpParty 对象向我的 rails API 发送请求:
HTTParty.get(task_list_url(@hotel_id),
:headers => { 'X-User-Email' => @user_email, 'X-User-Token'=> @user_token, 'Content-Type' => 'application/json' }
)
我将此对象存储在@response 变量中
API 上的方法看起来像那样
def index
@tasks = policy_scope(Task).where(hotel: @hotel)
if current_user.created_account == @hotel.account || current_user.hotels.include?(@hotel)
render json: {tasks: @tasks, hotel: @hotel}
else
render json: {
:status => :unauthorized,
:message => "unauthorized"
}
end
end
问题出在其他情况下,@response returned 看起来像这样:
#<HTTParty::Response:0x7fbb0a580198 parsed_response={"status"=>"unauthorized", "message"=>"unauthorized"}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "x-content-type-options"=>["nosniff"], "access-control-allow-origin"=>["*"], "access-control-allow-methods"=>["POST, GET, PUT, DELETE, OPTIONS"], "access-control-allow-headers"=>["Origin, Content-Type, Accept, Authorization, Token"], "access-control-max-age"=>["1728000"], "content-type"=>["application/json; charset=utf-8"], "etag"=>["W/\"2cd59ce00f09c29553693183e9a04a4d\""], "cache-control"=>["max-age=0, private, must-revalidate"], "x-request-id"=>["5acc353c-48aa-410d-a4bc-888c653cae02"], "x-runtime"=>["0.459027"], "vary"=>["Origin"], "connection"=>["close"], "transfer-encoding"=>["chunked"]}>
相关信息不在@response中,如您所见,而是在parsed_response
中。这在我的客户端应用程序中产生了一个问题,因为我正在检查 @response returns OK 200 而正确的 return 应该是 UNAUTHORIZED 401.
如何将正确的 json 响应放入 @response 对象中,或者如果不可能,如何检查 HTTParty 对象中的 parsed_response。
控制器的这个小修正应该可以解决问题。
而不是:
render json: {
:status => :unauthorized,
:message => "unauthorized"
}
使用这个:
render json: { message: "unauthorized" }, status: :unauthorized