Ruby-on-rails 如何在 Rails 中解析传入的 POST 请求的 json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4360008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:48:56  来源:igfitidea点击:

How to parse json of an incoming POST request in Rails?

ruby-on-railsposthttp-post

提问by thomas8877

I have the following problem. A web service is sending a JSON POST request to my app and I want to parse it.

我有以下问题。Web 服务正在向我的应用程序发送 JSON POST 请求,我想解析它。

I thought I just can access the params with

我以为我只能访问参数

@var = params[:name_of_the_JSON_fields]

but it doesn't work. I see in my Heroku logs, that the request is done and that the parameters are there, but I just can't store them.

但它不起作用。我在 Heroku 日志中看到,请求已完成并且参数在那里,但我无法存储它们。

Does anyone have an idea?

有没有人有想法?

回答by Jesse Wolgamott

When you post JSON (or XML), rails will handle all of the parsing for you, but you need to include the correct headers.

当您发布 JSON(或 XML)时,rails 将为您处理所有解析,但您需要包含正确的标头。

Have your app include:

让您的应用包括:

Content-type: application/json

And all will be cool.

一切都会很酷。

回答by Matte3o

This answer may not be particular to this exact question, but I had a similar issue when setting up AWS SNS push notifications. I was unable to parse or even view the initial subscription request. Hopefully this helps someone else with a similar problem.

这个答案可能不是针对这个确切的问题,但我在设置 AWS SNS 推送通知时遇到了类似的问题。我无法解析甚至查看初始订阅请求。希望这可以帮助其他有类似问题的人。

I found that you don't need to parse if you have a simple API setup with default format set to json, similar to below (in config/routes.rb):

我发现如果你有一个简单的 API 设置,默认格式设置为 json,你不需要解析,类似于下面(在 config/routes.rb 中):

 namespace :api, defaults: {format: :json}  do
    namespace :v1 do
      post "/controller_name" => 'controller_name#create'
      get "/controller_name" => 'controller_name#index'
    end
  end

The important thing I discovered is that the incoming post request comes is accessible by the variable request. In order to convert this into readable JSON format, you can call the following:

我发现的重要事情是传入的 post 请求可以通过变量访问request。为了将其转换为可读的 JSON 格式,您可以调用以下代码:

request.body.read()

回答by zetetic

If you are receiving JSON in the params hash you can convert it yourself:

如果您在 params 散列中收到 JSON,您可以自己转换它:

@var = JSON.parse(params[:name_of_the_JSON_fields])

回答by mltsy

In most cases with the symptom desribed, the true root of the problem is the source of the incoming request: if it is sending JSON to your app, it should be sending a Content-Type header of application/json.

在出现上述症状的大多数情况下,问题的真正根源在于传入请求的来源:如果它向您的应用程序发送 JSON,则它应该发送application/json.

If you're able to modify the app sending the request, adjust that header, and Rails will parse the body as JSON, and everything will work as expected, with the parsed JSON fields appearing in your paramshash.

如果您能够修改发送请求的应用程序,请调整该标头,Rails 会将正文解析为 JSON,一切都会按预期工作,解析的 JSON 字段出现在您的params哈希中。

When that is not possible (for instance when you don't control the source of the request - as in the case of receiving an Amazon SNS notification), Rails will not automatically parse the body as JSON for you, so the best you can to is read and parse it yourself, as in:

如果这是不可能的(例如,当您无法控制请求的来源时 - 如在接收 Amazon SNS 通知的情况下),Rails 不会自动将正文解析为 JSON,因此您可以尽最大努力自己阅读并解析它,如下所示:

json_params = JSON.parse(request.raw_post)

回答by Kevin

Probably too late to help you, but perhaps future people will check here :) Perhaps rails is supposed to parse json for you, but that's never worked for me. I read the request body directly. I use the 'Yajl' json parser - it is very fast. But regular old 'json' will work here too (just use JSON.parse)

可能太晚了,帮不了你,但也许未来的人会在这里检查:) 也许 rails 应该为你解析 json,但这对我来说从来没有用过。我直接阅读了请求正文。我使用 'Yajl' json 解析器 - 它非常快。但是常规的旧“json”也可以在这里工作(只需使用 JSON.parse)

request.body.rewind
body = Yajl::Parser.parse request.body.read.html_safe