Ana*_*and 4 ruby sinatra rest-client
我试图通过RestClient ruby API将JSON数据发送到Sinatra应用程序.
在客户端(client.rb)(使用RestClient API)
response = RestClient.post 'http://localhost:4567/solve', jdata, :content_type => :json, :accept => :json
Run Code Online (Sandbox Code Playgroud)
在服务器(Sinatra)
require "rubygems"
require "sinatra"
post '/solve/:data' do
jdata = params[:data]
for_json = JSON.parse(jdata)
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
/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
Run Code Online (Sandbox Code Playgroud)
我想要的是发送JSON数据并使用RestClient和Sinatra接收JSON数据.但是无论我尝试什么,我都会收到上述错误.我坚持了3个小时.请帮忙
shi*_*ara 14
您的sinatra应用程序与http:// localhost:4567/solve URL 不匹配,因此它从您的服务器返回404.
您需要通过示例更改您的sinatra应用程序:
require "rubygems"
require "sinatra"
post '/solve/?' do
jdata = params[:data]
for_json = JSON.parse(jdata)
end
Run Code Online (Sandbox Code Playgroud)
您的RestClient请求也有问题.您需要定义jdata的params名称.
response = RestClient.post 'http://localhost:4567/solve', {:data => jdata}, {:content_type => :json, :accept => :json}
Run Code Online (Sandbox Code Playgroud)