From b0623b7dc857fccadf663d3aec623b2bf997ece7 Mon Sep 17 00:00:00 2001 From: rudolfkoenig Date: Thu, 13 Feb 2014 15:10:26 +0000 Subject: [PATCH] HttpUtils: "POST data as hash" patch from justme1968 git-svn-id: https://svn.fhem.de/fhem/trunk@4909 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/HttpUtils.pm | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fhem/FHEM/HttpUtils.pm b/fhem/FHEM/HttpUtils.pm index 6eb415185..115da963e 100644 --- a/fhem/FHEM/HttpUtils.pm +++ b/fhem/FHEM/HttpUtils.pm @@ -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