Typhoeus 中的负载 Ruby 删除请求

Payload in Typhoeus Ruby Delete Request

我正在尝试使用 Typhoeus Delete 调用发送请求负载(如在 post 调用中)。 据我所知,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.

我试过这段代码,但是 body/payload 无法检索

    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

让我引用 specification:

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.

我不会说它可以被称为使用 DELETE 请求发送负载的明确许可。它告诉您可以发送有效负载,但此类请求的处理完全由服务器自行决定。

事情是这样的:

At the other side, it comes in an encoded way, where I can not retrieve it

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

我终于查看了我所有的负载请求(POST 和 PUT),发现我没有发送 headers 和这个 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)

只需向其中添加 headers,即可正常运行