使用 RestClient 和 Sinatra 发送和接收 JSON
我正在尝试通过 RestClient ruby API 将 JSON 数据发送到 Sinatra 应用程序。
在客户端(client.rb)(使用RestClient API)
response = RestClient.post 'http://localhost:4567/solve', jdata, :content_type => :json, :accept => :json
在服务器(Sinatra)
require "rubygems"
require "sinatra"
post '/solve/:data' do
jdata = params[:data]
for_json = JSON.parse(jdata)
end
我收到以下错误
/Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/abstract_response.rb:53:in `return!': Resource Not Found (RestClient::ResourceNotFound)
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:193:in `process_result'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:142:in `transmit'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:543:in `start'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:139:in `transmit'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:56:in `execute'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:31:in `execute'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient.rb:72:in `post'
from client.rb:52
我想要的只是使用RestClient和Sinatra发送JSON数据并接收返回的JSON数据..但是无论我尝试什么,我都会得到以上错误。我被这个问题困扰了3个小时。请帮忙
I am trying to send a JSON data to a Sinatra app by RestClient ruby API.
At client(client.rb) (using RestClient API)
response = RestClient.post 'http://localhost:4567/solve', jdata, :content_type => :json, :accept => :json
At server (Sinatra)
require "rubygems"
require "sinatra"
post '/solve/:data' do
jdata = params[:data]
for_json = JSON.parse(jdata)
end
I get the following error
/Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/abstract_response.rb:53:in `return!': Resource Not Found (RestClient::ResourceNotFound)
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:193:in `process_result'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:142:in `transmit'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:543:in `start'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:139:in `transmit'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:56:in `execute'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:31:in `execute'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient.rb:72:in `post'
from client.rb:52
All I want is to send JSON data and receive a JSON data back using RestClient and Sinatra..but whatever I try, I get the above error. I m stuck with this for 3 hours. Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 sinatra 应用程序与 http://localhost:4567/solve URL 不匹配,因此返回 404从您的服务器。
您需要通过示例更改您的 sinatra 应用程序:
您的 RestClient 请求也有问题。您需要定义 jdata 的参数名称。
Your sinatra app, don't match with http://localhost:4567/solve URL, so it's return a 404 from your server.
You need change your sinatra app by example :
You have a problem with your RestClient request too. You need define the params name of jdata.
试试这个:
然后试试这个:
我没有测试它,但也许你应该将 json 数据作为值而不是键发送。您的数据可能如下所示:{:key => '我是一个价值'} =>;零。您的数据根本不必位于网址中。您不需要 /solve/:data url。 POST 值不会在 url 中发送
调试在路由中收到的内容的一个好方法是打印参数:
希望这有帮助!
Try this:
And then try this:
I didn't test it but maybe you should send the json data as value rather than a key. Is is possible that you data looks like this: {:key => 'I am a value'} => nil. Your data does not necessary has to be in url at all. You don't need /solve/:data url. POST values are not to be send in url
A good way to debug what you receive in your route is to print the params:
Hope this helps!