如何在Rails中解析传入的POST请求的json?

时间:2022-10-24 12:50:24

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

我以为我可以用它来访问params

@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?

有没有人有想法?

4 个解决方案

#1


21  

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.

一切都会很酷。

#2


9  

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])

#3


7  

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请求可以通过变量请求访问。为了将其转换为可读的JSON格式,您可以调用以下内容:

request.body.read()

#4


4  

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

#1


21  

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.

一切都会很酷。

#2


9  

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])

#3


7  

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请求可以通过变量请求访问。为了将其转换为可读的JSON格式,您可以调用以下内容:

request.body.read()

#4


4  

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