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

问题描述 投票:0回答:2

我正在尝试使用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
ruby ruby-on-rails-4.2 http-delete typhoeus
2个回答
0
投票

让我引用specification

DELETE请求消息中的有效负载没有定义的语义;在DELETE请求上发送有效负载主体可能会导致某些现有实现拒绝该请求。

我不会说它可以作为使用DELETE请求发送有效负载的显式权限来调用。它告诉您可以发送有效负载,但处理此类请求完全由服务器决定。

这就是发生的事情:

另一方面,它采用编码方式,我无法检索它

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


0
投票

我终于查看了我的所有请求,其中我是有效负载(POST和PUT),并观察到我没有发送标头以及此DELETE请求。

它看起来像这样:

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)

只需添加标题,就可以了

© www.soinside.com 2019 - 2024. All rights reserved.