Payload in Typhoeus Ruby Delete Request

512 views Asked by At

I am trying to send request payload(like in a post call) with Typhoeus Delete call. As far as I know, The latest update to the HTTP 1.1 specification (RFC 7231) explicitly permits an entity body in a DELETE request:

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.

I tried this code, but body/payload is not retrievable

    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)

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

Other side code is like :

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

There are 2 answers

0
Devyani On BEST ANSWER

I finally looked at all my requests in which I was payload (POST and PUT) and observed that I was not sending headers along with this DELETE request.

It looks something like this:

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)

Just adding headers to it, made it work

3
Ilya Konyukhov On

Let me cite the 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.

I would NOT say it can be called as explicit permission to send payload with DELETE request. It tells you MAY send a payload, but the processing of such a request remains entirely at the discretion of the server.

And this is what happens:

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

Why can't you send your payload as a part of POST request, which is guaranteed to be processed by the server normally?