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
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)
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.
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!
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