Making the HTTPObject post method work
by Quentin Headen · 12/16/2010 (2:29 pm) · 2 comments
NOTE: I have only used this code in TGE 1.5.2 and TGB 1.7.5. As for other engines, or other versions of these engines, I'm not sure how the HTTPObject code looks. If this code works in an engine other than what I stated, please leave and comment so others will know.
While making a game, I wanted to add in a simple leaderboard component. After picking around with the HTTPObject, I realized that its post method was pretty busted. I'm not sure if i was reading the code wrong, but it even seemed that although you call the post method, the HTTPObject would always do a GET request no matter if you called get() or post(). Some members have simply used the TCPObject, but it is easier to have an object handle the intricate HTTP request details for you. So below are the steps I used to fix POST support in the HTTPObject.
1) In TGB or TGE, go the "game/net" (net is a sub-folder under game) folder in the engine's source directory.
2) Once there, open up the file httpObject.cc.
3) Locate the HTTPObject::onConnected method and find the piece of code in it that looks like this:
4) Replace that code with this:
5) Now scroll down to the very bottom and find this section of code:
6) Replace that code with this:
That's all there is to it!! Now recompile the engine to include your new changes.
Lets just say you wanted to do a post request to www.example.com/myscript.php with the parameters name = Tom age = 20. You could call it like this
I hope this script helps everyone. Enjoy!
While making a game, I wanted to add in a simple leaderboard component. After picking around with the HTTPObject, I realized that its post method was pretty busted. I'm not sure if i was reading the code wrong, but it even seemed that although you call the post method, the HTTPObject would always do a GET request no matter if you called get() or post(). Some members have simply used the TCPObject, but it is easier to have an object handle the intricate HTTP request details for you. So below are the steps I used to fix POST support in the HTTPObject.
1) In TGB or TGE, go the "game/net" (net is a sub-folder under game) folder in the engine's source directory.
2) Once there, open up the file httpObject.cc.
3) Locate the HTTPObject::onConnected method and find the piece of code in it that looks like this:
char *pt = dStrchr(mHostName, ':');
if(pt)
*pt = 0;
dSprintf(buffer, sizeof(buffer), "GET %s HTTP/1.1rnHost: %srnrn", expPath, mHostName);
if(pt)
*pt = ':';4) Replace that code with this:
char *pt = dStrchr(mHostName, ':');
if(pt)
*pt = 0;
//If we want to do a get request
if(mPost == NULL)
{
dSprintf(buffer, sizeof(buffer), "GET %s HTTP/1.1rnHost: %srnrn", expPath, mHostName);
}
//Else if we want to do a post request
else
{
dSprintf(buffer, sizeof(buffer), "POST %s HTTP/1.1rnHost: %srnContent-Type: application/x-www-form-urlencodedrnContent-Length: %irnrn%srnrn",
expPath, mHostName, dStrlen(mPost), mPost);
}
if(pt)
*pt = ':';5) Now scroll down to the very bottom and find this section of code:
ConsoleMethod( HTTPObject, post, void, 6, 6, "(TransportAddress addr, string requestURI, string query, string post)")
{
object->post(argv[2], argv[3], argv[4], argv[5]);
}6) Replace that code with this:
ConsoleMethod( HTTPObject, post, void, 5, 5, "(TransportAddress addr, string requestURI, string postData (without starting ?) )")
{
object->post(argv[2], argv[3], NULL, argv[4]);
}That's all there is to it!! Now recompile the engine to include your new changes.
Lets just say you wanted to do a post request to www.example.com/myscript.php with the parameters name = Tom age = 20. You could call it like this
http.post("www.example.com:80", "/myscript.php", "name=Tom&age=20") After you call that, the engine will send a post request with the given data to the specified script. Remember "http" needs to be replaced with the name or ID of your HTTPObject.I hope this script helps everyone. Enjoy!
About the author
Just your average programmer who tries to finish the projects he starts. :) I am currently focused on creating games with Torque engines. My website is http://phaseshiftsoftware.com
#2
11/07/2014 (5:12 pm)
Thanks for this! Can't believe this is still unfixed. Made an issue, and hope to get it resolved today. 
Torque 3D Owner Guimo
Just joking!!! Very good job!!! :)
Things like this should be integrated in the engine by default.