2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 23:06:37 +00:00

HttpUtils: "POST data as hash" patch from justme1968

git-svn-id: https://svn.fhem.de/fhem/trunk@4909 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2014-02-13 15:10:26 +00:00
parent 353719c207
commit c925a10616

View File

@ -171,19 +171,31 @@ HttpUtils_Connect2($)
return "$hash->{displayurl}: Can't connect to $hash->{addr}: $@";
}
my $data;
if(defined($hash->{data})) {
if( ref($hash->{data}) eq 'HASH' ) {
foreach my $key (keys %{$hash->{data}}) {
$data .= "&" if( $data );
$data .= "$key=". urlEncode($hash->{data}{$key});
}
} else {
$data = $hash->{data};
}
}
$hash->{host} =~ s/:.*//;
my $hdr = ($hash->{data} ? "POST" : "GET")." $hash->{path} HTTP/1.0\r\n";
my $hdr = ($data ? "POST" : "GET")." $hash->{path} HTTP/1.0\r\n";
$hdr .= "Host: $hash->{host}\r\n";
$hdr .= "Authorization: Basic $hash->{auth}\r\n" if(defined($hash->{auth}));
$hdr .= $hash->{header}."\r\n" if(defined($hash->{header}));
if(defined($hash->{data})) {
$hdr .= "Content-Length: ".length($hash->{data})."\r\n";
if(defined($data)) {
$hdr .= "Content-Length: ".length($data)."\r\n";
$hdr .= "Content-Type: application/x-www-form-urlencoded\r\n"
if ($hdr !~ "Content-Type:");
}
$hdr .= "\r\n";
syswrite $hash->{conn}, $hdr;
syswrite $hash->{conn}, $hash->{data} if(defined($hash->{data}));
syswrite $hash->{conn}, $data if(defined($data));
shutdown $hash->{conn}, 1 if(!$hash->{noshutdown});
if($hash->{callback}) { # Nonblocking read