Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

llHTTPRequest to simulate a form POST

Kermitt Quirk
Registered User
Join date: 4 Sep 2004
Posts: 267
05-25-2006 16:16
Has anyone managed to get llHTTPRequest to perform a post, as if the request had come from a form submit button? From my understanding the request should be something like this...
CODE

string url = "http://myurl.com";
string params = [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"];
string body = "param1=value1&param2=value2";
llHTTPRequest(url, params, body);

The problem I've having seems to be with setting the mimetype. Even though the syntax highlighting in LSL shows HTTP_MIMETYPE as a valid constant, when I try to run the script I get an error which suggests that HTTP_MIMETYPE is not a valid value to use in the parameters list. I don't have the exact error message handy atm, but I'll try to remember to post it later tonight if I haven't seen any solution by then.
There are other ways I can do this but I'd prefer to be able to use the default code that handles the form submissions if possible, rather than having to re-code all the server side stuff to use some other method that would become SL specific.
Jarod Godel
Utilitarian
Join date: 6 Nov 2003
Posts: 729
05-25-2006 16:31
From: Kermitt Quirk
Has anyone managed to get llHTTPRequest to perform a post, as if the request had come from a form submit button?
I have not. I didn't fiddle with the Mimetype, but post did not send a value.
_____________________
"All designers in SL need to be aware of the fact that there are now quite simple methods of complete texture theft in SL that are impossible to stop..." - Cristiano Midnight

Ad aspera per intelligentem prohibitus.
Kurt Zidane
Just Human
Join date: 1 Apr 2004
Posts: 636
05-25-2006 16:53
some thing like
url = "http://myurl.com/index.html?param1=value
maybe?
Jarod Godel
Utilitarian
Join date: 6 Nov 2003
Posts: 729
05-25-2006 17:13
That's typically a GET request.
_____________________
"All designers in SL need to be aware of the fact that there are now quite simple methods of complete texture theft in SL that are impossible to stop..." - Cristiano Midnight

Ad aspera per intelligentem prohibitus.
Jim Bunderfeld
The Coder
Join date: 1 Mar 2004
Posts: 161
05-25-2006 17:14
Damn, I just have to biggest problems with PHP. I have never learned another language besides LSL.
NeoBokrug Elytis
Master Blaster
Join date: 1 Nov 2005
Posts: 35
05-25-2006 17:47
llHTTPRequest("URL GOES HERE", [HTTP_METHOD, "POST"], "Variable1=postvariable1&Variable2=postvariable2";);

The Wiki has a post parser thing for PHP.
http://secondlife.com/badgeo/wakka.php?wakka=llHTTPRequest&show_comments=1#comments


That seems to work for me.
Kermitt Quirk
Registered User
Join date: 4 Sep 2004
Posts: 267
05-25-2006 20:25
From: NeoBokrug Elytis
llHTTPRequest("URL GOES HERE", [HTTP_METHOD, "POST"], "Variable1=postvariable1&Variable2=postvariable2";);

I'll double check when I get home, but I'm pretty sure I tried that and it gave me a HTTP 500 response. That's why I started trying to set the mime type.
Kermitt Quirk
Registered User
Join date: 4 Sep 2004
Posts: 267
05-25-2006 20:29
From: NeoBokrug Elytis
llHTTPRequest("URL GOES HERE", [HTTP_METHOD, "POST"], "Variable1=postvariable1&Variable2=postvariable2";);

I'll double check when I get home, but I'm pretty sure I tried that and it gave me a HTTP 500 response. That's why I started trying to set the mime type.

Even if that's the case though, the question still remains as to why the llHTTPRequest won't accept HTTP_MIMETYPE as a parameter because clearly according to the editor it's a valid constant.
Kermitt Quirk
Registered User
Join date: 4 Sep 2004
Posts: 267
05-26-2006 04:05
I think u may be right, the mime type isn't needed. I've managed to get a HTTP 200 back by excluding the mimetype and blocking out a bit of of the server side code temporarily. It's starting to look like there's something wrong with the post parameters I'm sending. Don't have any more time to work on it tonight but hopefully I'll get it working this weekend.

BTW... the error I was getting when I included HTTP_MIMETYPE was...
HTTP parameter key 2 is not recognized
Androclese Antonelli
Org. B-Day: 05/11/04
Join date: 25 Apr 2006
Posts: 96
05-26-2006 06:42
The only protection that POST gives you over GET is that the values won't be in the URL when it it send to your server. It gives you no other value.

When building a website, I will post most of my information through a POST instead of GET, but is just to keep the URL clean on the browser.

For this LSL=>PHP deal, there is nothing gained by using POST over GET since it all happens "behind the scenes" at the server level. (e.g., no browser window to mess up).

Lets say you are sending this to your pHP script:

http://mydomain.net/SLdataparse.php?value1=foo&value2=bar

To read a GET value, you just use $_GET[foo] and $_GET[bar] to read those values.

If you absolutely must send it as a POST, use $_POST[foo] and $_POST[bar] to read the values.

Alternativly, if you want to hedge your bets and don't want to swap deal with typing, use $_REQUEST[foo] and $_REQUEST[bar].

(yes, I code PHP in RL)
_____________________
The Sculpted Garden

Originally Born 5/11/2004 - New AV, Old Player.

If you leave the game, don't delete your account, just make it 'free'. You'll lose your inventory like I did. *sniff*
yetihehe Saarinen
Registered beast
Join date: 19 Feb 2006
Posts: 40
05-26-2006 07:00
But post handling would be nice. I too think that get is sufficient for sl, but if post is possible, why it doesn't work? Looks like another parytially implemented feature, but this time I can do almost all I want with what we have.
Kelly Linden
Linden Developer
Join date: 29 Mar 2004
Posts: 896
05-26-2006 09:27
Simple echo script in perl to spit back any post params.
CODE
#!/usr/local/bin/perl
use CGI;

$cgi = CGI->new;

print $cgi->header;
my %form;
foreach my $p ($cgi->param())
{
$form{$p} = $cgi->param($p);
print "$p = $form{$p}\n";
}
My testing shows that 1 post param is sent, POSTDATA and it's value is the entire passed string.
_____________________
- Kelly Linden
Rickard Roentgen
Renaissance Punk
Join date: 4 Apr 2004
Posts: 1,869
05-26-2006 11:29
post is necessary because get is limited in size and character set.
_____________________
Lex Neva
wears dorky glasses
Join date: 27 Nov 2004
Posts: 1,361
05-26-2006 11:30
From: Kelly Linden
My testing shows that 1 post param is sent, POSTDATA and it's value is the entire passed string.


Right. If we wanted to actually submit to a post form, the mime type would have to be text/x-www-form-urlencode for the POST body, according to the spec, otherwise the program on the receiving end thinks it's just plain text that's being sent. It's easy enough to get around this limitation if you're writing your own form (in PHP, try parse_str()), but if someone wants to post to an existing form, they'll have a problem because of the mime type. It'd be cool if we could set the mime type to text/x-www-form-urlencode... or text/anything.
Sol Columbia
Ding! Level up
Join date: 24 Sep 2005
Posts: 91
05-26-2006 17:04
Maybe this will help. This sends an http post request (the get is commented) to a website and the website processes it and returns.


Put this in a box or something...

CODE


key req_id;

default
{
state_entry() {

}

touch_start(integer total_number) {
//req_id = llHTTPRequest("http://www.abc.com/sl/test.php?x=" + (string)llGetKey(), [HTTP_METHOD, "GET"], "");
req_id = llHTTPRequest("http://www.abc.com/sl/test.php", [HTTP_METHOD, "POST"], "x=" + (string)llDetectedKey(0));

}

http_response(key request_id, integer status, list metadata, string body) {
if (req_id == request_id) {
//llOwnerSay((string)status);
//llOwnerSay((string)metadata);
llSay(0, body);
}
}

}




Here is the PHP code

CODE


<?php

//Get POST variables and put into superglobal array
$p_data = implode('', file('php://input'));
$p_data = explode('&', $p_data);

foreach ($p_data as $p_val) {
$d_parts = explode('=', $p_val);
$_POST[$d_parts[0]] = urldecode($d_parts[1]);
}



$query = $_POST['x'];

$return = $query." yes";

echo $return;

?>



Clearly, you have to change the URL in the prim to the right spot for the php code. But, it works fine.

Cheers.
_____________________
Kermitt Quirk
Registered User
Join date: 4 Sep 2004
Posts: 267
05-26-2006 23:38
I've just got my form post working perfectly. In the end I didn't need mimetype. Seems that the original server side code for handling the form post just isn't paying any attention to that value. My problem was that the parameters had to be in a form like "user[firstname]=Kermitt&user[lastname]=Quirk" as opposed to just "firstname=Kermitt&lastname=Quirk". If I'd paid more attention the html for the form that was generated by my server code I probably woulda realised that days ago. (BTW... My server side code is Ruby not PHP). Many thanx to everyone that posted here. This thread definately helped confirm a lot of things that I'd suspected but hadn't been able to prove, so it made it a lot easier to isolate where I was going wrong.
Kitten Lulu
Registered User
Join date: 8 Jul 2005
Posts: 114
05-27-2006 02:16
Instead of using $_POST, you can use $HTTP_RAW_DATA to read the request body directly.

In any case, I prefer Python or Ruby over PHP for Web development.
_____________________
I've got a blog!

My products are available on SLExchange or in-world in my shops: Kitten Lulu's Emporium and Kitten&Co.
Clubside Granville
Registered Bonehead
Join date: 13 Apr 2006
Posts: 478
06-10-2006 17:03
Has anyone actually solved this using normal form access rather than raw header reading?

In ASP you have a simple request.form(var_name) method which doesn't detect anything when using the POST command and as the original poster pointed out the valid MIMETYPE is rejected.

I want to keep existing form code working as is and add Second Life as an alternative posting method.
_____________________
Second Life Home Page Forums - slhomepage.com

Second Life Handbook - slhandbook.com

Second Life Mainland - slmainland.com
SiRiS Asturias
Chaotic Coder
Join date: 27 Sep 2005
Posts: 93
06-12-2006 02:56
There is very simple solutions to both problems in this thread:


PHP - Instead of using $_POST, $_GET, $HTTP_RAW_DATA, or using this "workaround" floating around that implodes & explodes to repack the variables, simply use $_REQUEST. Works for both GET or POST... Viola!

EX:

Sending the following to your .PHP file:

CODE
 
key requestid;
default
{
touch(integer n)
{
if (llDetectedKey(0) == llGetOwner())
{
requestid = llHTTPRequest("http://www.YourDomain.com?Com=1",[HTTP_METHOD,"POST"],"");
}
}
http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == requestid)
{
if (body == "HTTPRecieved")
{
llOwnerSay("HTTP Sent Successfully!");
}
} else
llOwnerSay((string)status + " Error!");
}
}


Server side:
(Save as Default.php)

<?PHP
if ($_REQUEST['Com'] == '1') {
echo "HTTPRecieved";
}
?>

The above would return HTTPRecieved back to your LSL script.


ASP.Net - Since I am personally going the ASP.Net 2 route myself, the correct way to grab the data your looking for is as follows:

Instead of Request.Form which obviously is not going to work since your not POST'ing from a form, use Request.QueryString.

Example:

Let's say you POST the same way as above for PHP to your .ASPX file.

RequestID = ("http://www.YourDomain.com/Default.aspx?Com=1",[HTTP_METHOD,"POST"],"";);


On the server side to retrieve the variable use:

Request.QueryString("Com";)


Example code behind:
(Save as Default.aspx.vb)

CODE
PartialClass _Default 
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
If Request.QueryString("Com") = "1" Then
Response.Write("HTTPRecieved")
EndIf
End If
End Sub
EndClass


*Notice using the default Page_Load & making sure it isn't a PostBack gives you some assurance that it is a genuine query as opposed to a Form. Use "If Page.IsPostBack Then" for your Form POSTs, or simply add an Else.

Response.Write will then send back the info to your awaiting LSL script, in this case HTTPRecieved is sent back.


Enjoy! :p
_____________________
Proud founder of:
S3 - Self Storage Systems
S3storage.net (Coming Soon!)

SLBoutique.com
SLExchange.com - Find What You Need, When You Need It.

"Light travels faster than sound. This is why some people appear bright, until you hear them speak."
Kermitt Quirk
Registered User
Join date: 4 Sep 2004
Posts: 267
02-03-2007 20:48
Ruby on Rails used to be able to process the post params into the "params" array quite happily, and it would also always show the received params in the log. Recently I upgraded to the latest version of Ruby and Rails and suddenly none of my post params were getting through. It appears as though Rails didn't pay too much attention to the Content Type in the past, and now it is. Following SiRiS' lead, I've written a small function that parses the raw post data and populates the params array. To use this, add the function to your application controller, then when you need the parameters, make a call to this first, from within the function which processes the post response. The parameters will then all be available in the params array like they should be. Unfortunately the params will still not show up in the ruby logs which makes debugging just that much more of a pain. Note that this isn't necessary for a GET. Ruby still seems to populate the params array correctly for them without a need for the additional function below.

CODE
def parse_post_params
raw_params = request.raw_post().split('&')
raw_params.each do |raw_param|
raw_param_split = raw_param.split('=')
params[raw_param_split[0]] = raw_param_split[1]
end
end
Kracatoa Alonzo
Registered User
Join date: 31 Dec 2006
Posts: 9
Asp
02-08-2007 04:06
My I use ASP instead of PHP?

Tks