我想使用 Ruby 接收 JSON 对象。我写了这个:
require 'sinatra'
require 'json'
post '/' do
push = JSON.parse(params[:payload])
"I got some JSON: #{push.inspect}"
end
我正在发送:
var options = {
host: 'localhost',
port: 4567,
path: '/',
method: 'POST'
};
var myFirstJSON = { "payload" : { "headers" :
[{ "from" : from,
"to" : to,
"subject" : subject }],
"body" :
[{ "primeira_parte" : primeira_parte,
"segunda_parte" : segunda_parte,
"terceira_parte": terceira_parte }]
}};
req.write(JSON.stringify(myFirstJSON));
但是,我收到此错误:
TypeError - can't convert nil into String:
{"{\"payload\":{\"headers\":"=>{"{\"from\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f286978186b286978186dc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>\",\"to\":\"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a5e4f595e6a5e4f595e04494547" rel="noreferrer noopener nofollow">[email protected]</a>\",\"subject\":\"Testing\"}"=>{",\"body\":"=>{"{\"primeira_parte\":\"The following message to <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97e3f2e4e3d7e3f2e4e3b9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>> was undeliverable.\\r\\nThe reason for the problem:\\r\\n5.1.0 - Unknown address error 553-'sorry, this recipient is not in my valid"=>"\\r\\nrcptto list (#5.7.1)'\",\"segunda_parte\":\"Final-Recipient: rfc822"}}}, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5b1a0b6b185b1a0b6b1eba6aaa8" rel="noreferrer noopener nofollow">[email protected]</a>\\r\\nAction: failed\\r\\nStatus: 5.0.0 (permanent failure)\\r\\nRemote-MTA: dns"=>nil, "216.75.35.163"=>{"\\r\\nDiagnostic-Code: smtp"=>nil}, "5.1.0 - Unknown address error 553-'sorry, this recipient is not in my validrcptto list (#5.7.1)' (delivery attempts: 0)\",\"terceira_parte\":\"Received: from unknown (HELO aws-bacon-delivery-svc-iad-1020.vdc.amazon.com) ("=>{"10.144.21.123"=>{")\\r\\n by na-mm-outgoing-6102-bacon.iad6.amazon.com with ESMTP"=>nil}}, "16 Apr 2011 14:11:15 0000\\r\\nReturn-Path: 0000012f5eb1cab3-09564031-57ef-4136-8cd7-9f368c5acd7d-000000@email-bounces.amazonses.com\\r\\nDate: Sat, 16 Apr 2011 14:23:20 0000\\r\\nFrom: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccb8a5adaba38cb8a5adaba3bce2a3beab" rel="noreferrer noopener nofollow">[email protected]</a>\\r\\nTo: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b2a3b5b286b2a3b5b2e8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>\\r\\nMessage-ID: <0000012f5eb1cab3-09564031-57ef-4136-8cd7-9f368c5acd7d-000000@email.amazonses.com>\\r\\nSubject: Testing\\r\\nMime-Version: 1.0\\r\\nContent-Type: text/plain"=>nil, "\\r\\n charset"=>"UTF-8\\r\\nContent-Transfer-Encoding: 7bit\\r\\nX-AWS-Outgoing: 199.255.192.79\\r\\n\\r\\n<html><body><p> Helllooo </p></body></html>\\r\\n\\r\\n\"}]}}"}
最佳答案
你想要的是这样的路线:
post '/' do
push = JSON.parse(request.body.read)
"I got JSON: #{push.inspect}"
end
您没有对数据进行编码,因此它不会在参数中设置。
关于javascript - Ruby:接收并打印 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694139/