Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用头从散列呈现JSON_Ruby_Json_Api - Fatal编程技术网

Ruby 使用头从散列呈现JSON

Ruby 使用头从散列呈现JSON,ruby,json,api,Ruby,Json,Api,我需要在哈希中添加一个头,并将其转换为JSON 在我的控制器中,我有: render json: @rates @rates是如下所示的散列: {:rates=>[{:service_name=>"Standard", :service_code=>"FU", :total_price=>"1100", :currency=>"USD", :min_delivery_date=>"2016-03-11 08:00:00 +0000", :max_deli

我需要在哈希中添加一个头,并将其转换为JSON

在我的控制器中,我有:

render json: @rates
@rates
是如下所示的散列:

{:rates=>[{:service_name=>"Standard", :service_code=>"FU", 
:total_price=>"1100", :currency=>"USD", :min_delivery_date=>"2016-03-11 
08:00:00 +0000", :max_delivery_date=>"2016-03-16 06:59:59 +0000"}, 
{:service_name=>"Priority", :service_code=>"FU", :total_price=>"2300", 
:currency=>"USD", :min_delivery_date=>"2016-03-08 08:00:00 +0000", 
:max_delivery_date=>"2016-03-09 07:59:59 +0000"}, 
{:service_name=>"Expedited", :service_code=>"FU", :total_price=>"1420", 
:currency=>"USD", :min_delivery_date=>"2016-03-09 08:00:00 +0000", 
:max_delivery_date=>"2016-03-10 07:59:59 +0000"}]}
JSON格式是完美的,但我相信我需要一个标题,以便API从我这里获取速率。我看到了“”,但我不明白

我需要补充:

Content-Type: application/json

我可以把它作为另一个键/值对添加到哈希中吗?或者我必须做一些特殊的标题值吗?

您可以尝试添加内容类型选项

render json: @rates, content_type: "application/json"

render json:@rates
将自动设置内容类型。你什么都不需要做是的。。我显然做错了什么,因为我得到了那个奇怪的头球。试过了,但没有运气。。我希望有一些服务,我可以发布我的JSON,API可以点击它,看看它是否正确。。有人知道吗?我找到了!!这是我一直在寻找的服务:但是Hull.it要求您的应用程序可以公开访问。如果您正在开发JSON端点并在本地进行测试,那么只需使用Postman即可。天哪!那就更好了。。非常感谢。我只是想看看到底发生了什么。。我正在开发Cloud9,我开始认为它与此有关。。