sinatra 休息客户端 etag

发布于 2024-10-13 18:05:10 字数 1323 浏览 2 评论 0原文

我的目标是解决“丢失更新问题”(请参阅​​ 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 技术交流群。

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

发布评论

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

评论(1

惯饮孤独 2024-10-20 18:05:10

您的有效负载和标头位于同一哈希中。您必须在第二个哈希中指定 RestClient 的标头。尝试:

player = {"name" => "John Smith", "position" => "def"}.to_json
headers = {:content_type => :json, :if_none_match => '"something"'}

RestClient.put('http://localhost:4567/players/1', {:data => player}, headers) do |response, request, result, &block|
    p response.code.to_s + " " + response
end

我不确定 RestClient 是否正确翻译了标头。如果上述方法不起作用,请尝试使用:

headers = {'Content-Type' => :json, 'If-None-Match' => '"something"'}

Your payload and headers are in the same hash. You have to specify the headers for RestClient in second hash. Try:

player = {"name" => "John Smith", "position" => "def"}.to_json
headers = {:content_type => :json, :if_none_match => '"something"'}

RestClient.put('http://localhost:4567/players/1', {:data => player}, headers) do |response, request, result, &block|
    p response.code.to_s + " " + response
end

I am not sure if RestClient translates the headers correctly. If the above doesn't work, try it with:

headers = {'Content-Type' => :json, 'If-None-Match' => '"something"'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文