sinatra 休息客户端 etag
我的目标是解决“丢失更新问题”(请参阅 http://www.w3.org/ 1999/04/Editing/) 在 PUT 操作中。 我使用的是 sinatra,作为客户端我使用rest_client。我如何检查它是否有效?我的客户总是返回 200 代码。我是否使用了正确调用它的参数? (PUT 本身有效)
sinatra 代码:
put '/players/:id' do |id|
etag 'something'
if Player[id].nil? then
halt 404
end
begin
data = JSON.parse(params[:data])
pl = Player[id]
pl.name = data['name']
pl.position = data['position']
if pl.save
"Resource modified."
else
status 412
redirect '/players'
end
rescue Sequel::DatabaseError
409 #Conflict - The request was unsuccessful due to a conflict in the state of the resource.
rescue Exception => e
400
puts e.message
end
end
客户端调用:
player = {"name" => "John Smith", "position" => "def"}.to_json
RestClient.put('http://localhost:4567/players/1', {:data => player, :content_type => :json, :if_none_match => '"something"'}){ |response, request, result, &block|
p response.code.to_s + " " + response
}
我已经尝试放置 :if_none_match => “某事”,我尝试过:if_match。没有任何改变。 如何将标头放入 RestClient 请求中? 如何获得不同于200状态的某物? (即304未修改)?
My goal is to address "lost update problem" (see http://www.w3.org/1999/04/Editing/) in PUT operations.
I am using sinatra, and as a client I use rest_client. How I can check if it works? My client always return 200 code. Am I using arguments of calling it correctly? (PUT itself works)
sinatra code:
put '/players/:id' do |id|
etag 'something'
if Player[id].nil? then
halt 404
end
begin
data = JSON.parse(params[:data])
pl = Player[id]
pl.name = data['name']
pl.position = data['position']
if pl.save
"Resource modified."
else
status 412
redirect '/players'
end
rescue Sequel::DatabaseError
409 #Conflict - The request was unsuccessful due to a conflict in the state of the resource.
rescue Exception => e
400
puts e.message
end
end
client invocation:
player = {"name" => "John Smith", "position" => "def"}.to_json
RestClient.put('http://localhost:4567/players/1', {:data => player, :content_type => :json, :if_none_match => '"something"'}){ |response, request, result, &block|
p response.code.to_s + " " + response
}
I tried already to put :if_none_match => "something", I tried :if_match. Nothing changes.
How I can put headers into RestClient request?
How to obtain sth different than 200 status? (ie 304 not modified)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的有效负载和标头位于同一哈希中。您必须在第二个哈希中指定
RestClient
的标头。尝试:我不确定
RestClient
是否正确翻译了标头。如果上述方法不起作用,请尝试使用:Your payload and headers are in the same hash. You have to specify the headers for
RestClient
in second hash. Try:I am not sure if
RestClient
translates the headers correctly. If the above doesn't work, try it with: