1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Example use of the Curl module
18 module curl_http
is example
22 # Custom delegate to receive callbacks from a Curl transfer
26 # Body of the downloaded file
29 redef fun header_callback
(line
) do
30 # We keep this callback silent for testing purposes
31 #if not line.has_prefix("Date:") then print "Header_callback: {line}"
34 redef fun body_callback
(line
) do self.fetched_body
+= line
36 redef fun stream_callback
(buffer
) do print
"Stream_callback: {buffer}"
39 private fun print_usage
do print
"Usage: curl_http [POST|GET|GET_FILE] url"
41 if args
.length
< 2 then
47 var request
= new CurlHTTPRequest(url
)
48 request
.verbose
= false # Set to `true` to debug
50 if args
[0] == "GET" then
52 var response
= request
.execute
54 if response
isa CurlResponseSuccess then
55 print
"Status code: {response.status_code}"
56 print
"Body: {response.body_str}"
57 else if response
isa CurlResponseFailed then
58 print
"Error code: {response.error_code}"
59 print
"Error msg: {response.error_msg}"
62 else if args
[0] == "POST" then
64 var my_http_fetcher
= new MyHttpFetcher
65 request
.delegate
= my_http_fetcher
67 var post_data
= new HeaderMap
68 post_data
["Bugs Bunny"] = "Daffy Duck"
69 post_data
["Batman"] = "Robin likes special characters @#ùà!è§'(\"é
&://,;<>∞~
*"
70 post_data["Batman"] = "Yes you can set multiple identical keys
, but
APACHE will consider only one
, the last one
"
71 request.data = post_data
72 var response = request.execute
74 print "Our body from the callback
: {my_http_fetcher.fetched_body}"
76 if response isa CurlResponseSuccess then
77 print "*** Answer ***"
78 print "Status code
: {response.status_code}"
79 print "Body should be empty
, because we decided to manage callbacks
: {response.body_str.length}"
80 else if response isa CurlResponseFailed then
81 print "Error code
: {response.error_code}"
82 print "Error msg
: {response.error_msg}"
85 else if args[0] == "GET_FILE" then
86 # HTTP Get to file Request
87 var headers = new HeaderMap
88 headers["Accept"] = "Moo"
89 request.headers = headers
90 var response = request.download_to_file(null)
92 if response isa CurlFileResponseSuccess then
93 print "*** Answer ***"
94 print "Status code
: {response.status_code}"
95 print "Size downloaded
: {response.size_download}"
96 else if response isa CurlResponseFailed then
97 print "Error code
: {response.error_code}"
98 print "Error msg
: {response.error_msg}"