… | |
… | |
28 | limited support. |
28 | limited support. |
29 | |
29 | |
30 | METHODS |
30 | METHODS |
31 | http_get $url, key => value..., $cb->($data, $headers) |
31 | http_get $url, key => value..., $cb->($data, $headers) |
32 | Executes an HTTP-GET request. See the http_request function for |
32 | Executes an HTTP-GET request. See the http_request function for |
33 | details on additional parameters. |
33 | details on additional parameters and the return value. |
34 | |
34 | |
35 | http_head $url, key => value..., $cb->($data, $headers) |
35 | http_head $url, key => value..., $cb->($data, $headers) |
36 | Executes an HTTP-HEAD request. See the http_request function for |
36 | Executes an HTTP-HEAD request. See the http_request function for |
37 | details on additional parameters. |
37 | details on additional parameters and the return value. |
38 | |
38 | |
39 | http_post $url, $body, key => value..., $cb->($data, $headers) |
39 | http_post $url, $body, key => value..., $cb->($data, $headers) |
40 | Executes an HTTP-POST request with a request body of $body. See the |
40 | Executes an HTTP-POST request with a request body of $body. See the |
41 | http_request function for details on additional parameters. |
41 | http_request function for details on additional parameters and the |
|
|
42 | return value. |
42 | |
43 | |
43 | http_request $method => $url, key => value..., $cb->($data, $headers) |
44 | http_request $method => $url, key => value..., $cb->($data, $headers) |
44 | Executes a HTTP request of type $method (e.g. "GET", "POST"). The |
45 | Executes a HTTP request of type $method (e.g. "GET", "POST"). The |
45 | URL must be an absolute http or https URL. |
46 | URL must be an absolute http or https URL. |
|
|
47 | |
|
|
48 | When called in void context, nothing is returned. In other contexts, |
|
|
49 | "http_request" returns a "cancellation guard" - you have to keep the |
|
|
50 | object at least alive until the callback get called. If the object |
|
|
51 | gets destroyed before the callbakc is called, the request will be |
|
|
52 | cancelled. |
46 | |
53 | |
47 | The callback will be called with the response data as first argument |
54 | The callback will be called with the response data as first argument |
48 | (or "undef" if it wasn't available due to errors), and a hash-ref |
55 | (or "undef" if it wasn't available due to errors), and a hash-ref |
49 | with response headers as second argument. |
56 | with response headers as second argument. |
50 | |
57 | |
… | |
… | |
52 | response headers, the "pseudo-headers" "HTTPVersion", "Status" and |
59 | response headers, the "pseudo-headers" "HTTPVersion", "Status" and |
53 | "Reason" contain the three parts of the HTTP Status-Line of the same |
60 | "Reason" contain the three parts of the HTTP Status-Line of the same |
54 | name. The pseudo-header "URL" contains the original URL (which can |
61 | name. The pseudo-header "URL" contains the original URL (which can |
55 | differ from the requested URL when following redirects). |
62 | differ from the requested URL when following redirects). |
56 | |
63 | |
57 | If the server sends a header multiple lines, then their contents |
64 | If the server sends a header multiple times, then their contents |
58 | will be joined together with "\x00". |
65 | will be joined together with a comma (","), as per the HTTP spec. |
59 | |
66 | |
60 | If an internal error occurs, such as not being able to resolve a |
67 | If an internal error occurs, such as not being able to resolve a |
61 | hostname, then $data will be "undef", "$headers->{Status}" will be |
68 | hostname, then $data will be "undef", "$headers->{Status}" will be |
62 | 599 and the "Reason" pseudo-header will contain an error message. |
69 | 599 and the "Reason" pseudo-header will contain an error message. |
63 | |
70 | |
… | |
… | |
138 | use Data::Dumper; |
145 | use Data::Dumper; |
139 | print Dumper $hdr; |
146 | print Dumper $hdr; |
140 | } |
147 | } |
141 | ; |
148 | ; |
142 | |
149 | |
|
|
150 | Example: make another simple HTTP GET request, but immediately try |
|
|
151 | to cancel it. |
|
|
152 | |
|
|
153 | my $request = http_request GET => "http://www.nethype.de/", sub { |
|
|
154 | my ($body, $hdr) = @_; |
|
|
155 | print "$body\n"; |
|
|
156 | }; |
|
|
157 | |
|
|
158 | undef $request; |
|
|
159 | |
143 | GLOBAL FUNCTIONS AND VARIABLES |
160 | GLOBAL FUNCTIONS AND VARIABLES |
144 | AnyEvent::HTTP::set_proxy "proxy-url" |
161 | AnyEvent::HTTP::set_proxy "proxy-url" |
145 | Sets the default proxy server to use. The proxy-url must begin with |
162 | Sets the default proxy server to use. The proxy-url must begin with |
146 | a string of the form "http://host:port" (optionally "https:..."). |
163 | a string of the form "http://host:port" (optionally "https:..."). |
147 | |
164 | |