rubyruby-on-rails-4.2http-deletetyphoeus

Payload in Typhoeus Ruby Delete Request


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

Solution

  • 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