如何在Rails中解析传入的POST请求的json? [英] How to parse json of an incoming POST request in Rails?

查看:160
本文介绍了如何在Rails中解析传入的POST请求的json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我以为我可以使用

@var = params[:name_of_the_JSON_fields]

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

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.

有没有人有想法?

推荐答案

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

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

让您的应用包括:

Content-type: application/json

一切都很酷。

这篇关于如何在Rails中解析传入的POST请求的json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆