Typhoeus Ruby中的有效负载删除请求

时间:2018-10-23 10:28:30

标签: ruby ruby-on-rails-4.2 http-delete typhoeus

我正在尝试使用Typhoeus Delete调用发送请求有效负载(例如在后期调用中)。 据我所知,HTTP 1.1规范(RFC 7231)的最新更新明确允许DELETE请求中的实体主体:

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

我尝试了此代码,但正文/有效载荷无法检索

    query_body = {:bodyHash => body}

    request = Typhoeus::Request.new(
        url,
        body: JSON.dump(query_body),
        method: :delete,
        ssl_verifypeer: false,
        ssl_verifyhost: 0,
        verbose: true,
    )

    request.run
    response = request.response
    http_status = response.code
    response.total_time
    response.headers
    result = JSON.parse(response.body)

另一方面,它以一种编码方式出现,我无法检索到它

其他附带代码如:

def destroy
        respond_to do |format|
            format.json do
                body_hash = params[:bodyHash]
                #do stuff
                render json: {msg: 'User Successfully Logged out', status: 200}, status: :ok
            end
            format.all {render json: {msg: 'Only JSON types are supported', status: 406}.to_json, status: :ok}
        end
    end

2 个答案:

答案 0 :(得分:0)

让我引用specification

  

DELETE请求消息中的有效负载没有定义的语义;   在DELETE请求上发送有效内容正文可能会导致一些现有内容   拒绝请求的实现。

我不会说可以将其称为具有DELETE请求发送有效载荷的显式权限。它告诉您可以发送有效负载,但是这种请求的处理完全取决于服务器。

这就是发生的事情:

  

另一方面,它以编码方式出现,我无法检索到它

为什么您不能将有效负载作为POST请求的一部分发送,而该请求保证可以由服务器正常处理?

答案 1 :(得分:0)

最后,我查看了所有我属于有效负载的请求(POST和PUT),发现我没有与此删除请求一起发送标头。

它看起来像这样:

query_body = {:bodyHash => body}

    request = Typhoeus::Request.new(
        url,
        body: JSON.dump(query_body),
        method: :delete,
        ssl_verifypeer: false,
        ssl_verifyhost: 0,
        verbose: true,
        headers: {'X-Requested-With' => 'XMLHttpRequest', 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json, text/javascript, */*', 'enctype' => 'application/json'}
    )

    request.run
    response = request.response
    http_status = response.code
    response.total_time
    response.headers
    result = JSON.parse(response.body)

只需向其中添加标题,即可正常工作

相关问题