使用 RestClient 和 Sinatra 发送和接收 JSON

发布于 2024-09-04 17:24:29 字数 1414 浏览 11 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

廻憶裏菂餘溫 2024-09-11 17:24:29

您的 sinatra 应用程序与 http://localhost:4567/solve URL 不匹配,因此返回 404从您的服务器。

您需要通过示例更改您的 sinatra 应用程序:

require "rubygems"
require "sinatra"


post '/solve/?' do 
  jdata = params[:data]
  for_json = JSON.parse(jdata)
end

您的 RestClient 请求也有问题。您需要定义 jdata 的参数名称。

response = RestClient.post 'http://localhost:4567/solve', {:data => jdata}, {:content_type => :json, :accept => :json}

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 :

require "rubygems"
require "sinatra"


post '/solve/?' do 
  jdata = params[:data]
  for_json = JSON.parse(jdata)
end

You have a problem with your RestClient request too. You need define the params name of jdata.

response = RestClient.post 'http://localhost:4567/solve', {:data => jdata}, {:content_type => :json, :accept => :json}
千笙结 2024-09-11 17:24:29

试试这个:

jdata = {:key => 'I am a value'}.to_json    
response = RestClient.post 'http://localhost:4567/solve', :data => jdata, :content_type => :json, :accept => :json

然后试试这个:

post '/solve' do 
  jdata = JSON.parse(params[:data])
  puts jdata
end

我没有测试它,但也许你应该将 json 数据作为值而不是键发送。您的数据可能如下所示:{:key => '我是一个价值'} =>;零。您的数据根本不必位于网址中。您不需要 /solve/:data url。 POST 值不会在 url 中发送
调试在路由中收到的内容的一个好方法是打印参数:

puts params

希望这有帮助!

Try this:

jdata = {:key => 'I am a value'}.to_json    
response = RestClient.post 'http://localhost:4567/solve', :data => jdata, :content_type => :json, :accept => :json

And then try this:

post '/solve' do 
  jdata = JSON.parse(params[:data])
  puts jdata
end

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:

puts params

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文