简体   繁体   English

如何使用 Rails 从 API 获取 POST 请求

[英]How to get POST request from API with rails

I got POST request from API.我收到了来自 API 的 POST 请求。 I checked content-type, it's x-www-form-urlencoded, but body request also contains JSON.我检查了内容类型,它是 x-www-form-urlencoded,但正文请求也包含 JSON。 How can I parse from JSON in hash?如何从散列中的 JSON 解析?

Parameters of request in console: Screenshot ||控制台请求参数:截图|| Parameters: {"vpbx_api_key"=>"etxojfklr6nue6tl627pn5sdi0koov7t", "sign"=>"ad0c49034c8d83a7d7f1b433afc2ed5a9aa08d933dba3724062aed0c3d1a79bb", "json"=>"{\\"entry_id\\":\\"MjYyNjQ2NzM1Njo0Mg==\\",\\"call_id\\":\\"MToxMDAxNDAzOTo0Mjo4Mjc2NzEzMzk=\\",\\"timestamp\\":1485939839,\\"seq\\":2,\\"call_state\\":\\"Disconnected\\",\\"location\\":\\"abonent\\",\\"from\\":{\\"number\\":\\"79268220697\\",\\"taken_from_call_id\\":\\"MToxMDAxNDAzOTo0Mjo4Mjc2NzEwOTA6MQ==\\"},\\"to\\":{\\"extension\\":\\"2\\",\\"number\\":\\"79154612023\\",\\"line_number\\":\\"74953749768\\"},\\"disconnect_reason\\":1100}"}

If you want to keep the form encoded that way, you will have the parameters in the params collection.如果您想以这种方式保持表单编码,您将拥有 params 集合中的参数。 That means you could be parsing that JSON doing something as:这意味着您可以将 JSON 解析为:

def my_method_to_process_the_post
    parsed = JSON.parse params[:json]
end

'json' is the key for the json that you are receiving in the payload. 'json' 是您在有效负载中接收的 json 的键。

For historical evolution of JSON parsing you may want to check this thread.对于 JSON 解析的历史演变,您可能需要查看线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM