Http.request method PUT and DELETE doesn't send out any body data (DEF-2468) (FIXED)

Hi, I’m working on a REST API where I use the methods PUT and DELETE to update or delete a objects created in the server end.

What I discovered after a couple of hours of debugging is that http.request sends out a empty body request when using a “PUT” method. I’ve tried the same with curl and php and there it works as it should. I’ve also tried to replicated the headers sent out by the curl request and used it in the Defold http.request, but this didn’t help either.

This works:

	headers = {}
	headers["Content-Type"] = "application/x-www-form-urlencoded"
    headers["Accept"] = "*/*"
	request_body = tools.json_encode({["test"] = "value"})    
	http.request("http://localhost:80/index", "POST", function(self, id, response)
		pprint(response)
	end, headers, request_body)

But this doesn’t:

	headers = {}
	headers["Content-Type"] = "application/x-www-form-urlencoded"
    headers["Accept"] = "*/*"
	request_body = tools.json_encode({["test"] = "value"})    
	http.request("http://localhost:80/index", "PUT", function(self, id, response)
		pprint(response)
	end, headers, request_body)

…and the only difference is the request method.

Any ideas on how to solve it?
Thanks :slight_smile:

I remember @sicher mentioning that the only verbs that are supported are GET and POST. Anything else will become a GET. I can verify this tomorrow, unless @sicher can chime in now.

In any case, this is something that needs to be fixed. In the meantime it’s possible to use the http.lua module that comes bundled with LuaSocket.

That’s correct. Our http implementation does not work correctly for all methods.

Thanks for the quick reply, I’ll check out the http.lua.module.

I dug a bit deeper and for a PUT the body will be ignored and the request will work the same way as a GET but with the PUT as verb in the request header. A DELETE should work as expected. In any case, this needs to be fixed.

2 Likes

Thank you, I checked out the http lua module but have decided to wait for a fix for this issue instead. Do you know if this is something that will be fixed shortly or if it will take a couple of months? I don’t mean to be impatient, but it would help me prioritize my workload if I knew :slight_smile:

I’m looking into this one right now. If it’s a simple fix then it should be included in the next release.

1 Like

Any updates on how it went? Was it a simple fix?

Yes. I’ve updated the code to support PUT and HEAD. DELETE should already be supported. The code is in review and should hopefully make it into Defold 1.2.104.

1 Like

Awesome! Thanks :slight_smile:

And the new release has been slightly delayed since the entire company was away on our annual conference/kickoff/party.

Are you sure DELETE is supported? I don’t get any body data when making a DELETE request from defold to https://requestb.in.

And does your DELETE request contain a body or do you specify what to delete in the request URI? It seems to be debated wether a DELETE may contain a body or not, and in the Defold case any request body will be ignored when doing a DELETE.

Thank you for you fast reply.

I specify what to delete in the body. Not in the URI. From what I have read online it should be perfectly acceptable to make a DELETE request with a body. But I can change it so that I use the URI instead. But in my own opinion it should be up to the user to decide whether to do a body request. In my case I will have different game engines (not only Defold) that will speak to the server. And this forces me to change all the game engines to fit defolds DELETE request requirements. It is not that much work, but it could be a issue for someone else in the future.

I based my decision of letting DELETE work as it is (ie no body) on the spec:

“The DELETE method requests that the origin server delete the resource identified by the Request-URI”

But as I said, there’s quite a bit of debate surrounding DELETE and request bodies.

1 Like

Yeah, I read the debate link before making my last post. That was my source for when saying that it should be acceptable. Either way it’s okay by me whether you decide to keep it with no body.

1 Like

This feature has been released in Defold 1.2.104.

3 Likes