Http.request not sending POST data? (SOLVED)

I’ve set up a very simple script for capturing bug reports from users. I use http.request to access a php file on my server, which takes POST data and sends an email to a dedicated inbox I set up. I know the http.request fires because I receive an e-mail as expected, but the POST data (which I intend to be the body of the e-mail) does not seem to appear.

I have validated my php file by sending an http request using this site. The POST data I send there does appear in the e-mail that is generated.

I thought perhaps I was being sandboxed on my computer so I built an HTML5 version and uploaded it online, but the results were the same.

Here is the script I’ve produced:

local function bug_callback(self, id, response)

	print("bug callback", id)
	pprint(response)
end

function init(self)

	local args = "bug_report=bork!"
	http.request("myurlhere.php", "POST", bug_callback, nil, args)

end

Does anyone have any ideas?

Not sure why that wouldn’t work.
Try sending to another site, like “https://httpbin.org/post” or “https://ptsv2.com” to see if that works.

1 Like

Great idea, thanks. Love the toilet and the dumps… :man_facepalming: :laughing:

Here’s the dump where I used my validation site (linked above). Works fine:
https://ptsv2.com/t/dudcu-1592328429/d/471590782
image

And here’s the dump generated via my Defold script. The data is there, but stuck in “post body” rather than parameters like the other example:
https://ptsv2.com/t/dudcu-1592328429/d/477463579
image

So I’m guessing the issue has something to do with the formatting of the post_data parameter in my script:

local args = "bug_report=defold!"

But what? :thinking:

I got it, it was the headers parameter. I had a look at the dump of the post data from the website I was using to test, and noticed content-type was application/x-www-form-urlencoded. There was no such value in the Defold variant.

Updated my script to the following:

        local headers = {["Content-Type"] = " application/x-www-form-urlencoded"}
	local args = "bug_report=defold!"
	http.request("https://ptsv2.com/t/dudcu-1592328429/post", "POST", bug_callback, headers, args)

Which now yields:
https://ptsv2.com/t/dudcu-1592328429/d/479391290

Maybe something could be added to the documentation to explain this?

Hmm, not sure really. What should we mention specifically? We already document that you can send request headers and that you can send a request body. These things are part of the protocol and there’s nothing Defold specific about it.

The rest happens on the webserver which together with the request headers and the format of the request body decides how the entire HTTP request is interpreted.

2 Likes

Yeah, I guess you’re right. These things aren’t in the scope of Defold so wouldn’t make sense to document them. This thread now exists for people to search and find if they run into similar problems, so that’s good.

What I would say is that I personally like it when the documentation has multiple examples - one for the simplest, most frequent use case, and one to show the most extensive use of the feature.

As it stands, the example in the documentation is of the former kind. It uses GET (without passing any variables in the URL), and doesn’t use any of the optional parameters. I feel like an example that uses all parameters could be helpful.

The example in the documentation was enough to get me going, and figuring out how to send GET variables was trivial. Not sure if it was a flaw in my research on the subject but getting POST variables to appear was way trickier (as evidenced by the existence of this thread).

Now, I understand your point about documenting non-Defold material, and additionally, why would you use my problem as the example when any number of issues could happen (like this thread, which was a useful reference in solving my problem).

I guess I’m kind of just thinking out loud here… I think my opinion is that you are right not documenting things that aren’t Defold, but I stick by my desire to see both simple and complex examples. Perhaps that is too onerous, but I think it could have helped me out on my quest to figure this out!

1 Like

Hmm, I do think though that there is room for improvement in the Networking manual: https://defold.com/manuals/networking/

I’ll add a POST example there as well.

3 Likes

Hello @britzl

Thanks for adding the HTTP/POST manual.
However I’m stuck with sending multiple values. I tried a table but Defold rejected it stating it has to be a string. So I tried using delimiters, it didn’t work with : “,” nor “;” nor “&”

Here’s the code:

function M.add_name(uuid, name)
	local headers = {
		["Content-Type"] = " application/x-www-form-urlencoded"
	}
	body="uuid="..uuid.."&name="..name
	http.request(M.url, "POST", handle_response, headers, body)
end

The answer I’m getting is:

DEBUG:SCRIPT: settings gui: save:	d293e293-86b8-447d-b3be-42a59399d855	 - 	name surname
DEBUG:SCRIPT: NETWORK - received resp:	500	

Any idea?

Of course from the server part, it’s a simple answer I’m sending back:

<?php
echo "you sent me uuid:"; echo $_POST['uuid']
echo "you sent me name:"; echo $_POST['name']
?>

It did work with a single value, but not more

The output you posted doesn’t mention anything about a string?

It is a string?

Is name == name surname? If so it does have a space in it, which should be %20 if I’m not mistaken (since you tell it it is urlencoded)?

Have you tried using a separate tool to test?
E.g. uring curl

curl -d “param1=value1&param2=value2” -H “Content-Type: application/x-www-form-urlencoded” -X POST http://server/page

Hello Mathias,

I have removed the space. I’m still getting the same answer:

DEBUG:SCRIPT: body=	uuid=d293e293-86b8-447d-b3be-42a59399d855&name=rrr
DEBUG:SCRIPT: NETWORK - received resp:	500

I will try the curl and let you know asap. It might take a while. this is the first time I’m trying this on Defold…

Thanks a lot for the help Mathias,

It was an error on the server. It was syntax error (forget to use “;” at the end. I guess lua is affecting my php)

Thanks again
Regards,
Riad

2 Likes