Halite::Chainable
Instance methods
Accept the given MIME type
Halite.accept("application/json")
.get("http://httpbin.org/get")
Make a request with the given Authorization header
Halite.auth("private-token", "6abaef100b77808ceb7fe26a3bcff1d0")
.get("http://httpbin.org/get")
Make a request with the given Basic authorization header
Halite.basic_auth("icyleaf", "p@ssw0rd")
.get("http://httpbin.org/get")
See Also: http://tools.ietf.org/html/rfc2617
Make a request with the given cookies
Halite.cookies({"private-token", "6abaef100b77808ceb7fe26a3bcff1d0"})
.get("http://httpbin.org/get")
# Or
Halite.cookies({private-token: "6abaef100b77808ceb7fe26a3bcff1d0"})
.get("http://httpbin.org/get")
Make a request with the given cookies
cookies = HTTP::Cookies.from_client_headers(headers)
Halite.cookies(cookies)
.get("http://httpbin.org/get")
Make a request with the given cookies
Halite.cookies(name: "icyleaf", "gender": "male")
.get("http://httpbin.org/get")
Delete a resource
Request with form data
Halite.delete("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.delete("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.delete("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Delete a streaming resource
Halite.delete("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Adds a endpoint to the request.
Halite.endpoint("https://httpbin.org")
.get("/get")
Returns Options self with given max hops of redirect times.
# Max hops 3 times
Halite.follow(3)
.get("http://httpbin.org/relative-redirect/3")
# Always redirect with any request methods
Halite.follow(4, strict: false)
.get("http://httpbin.org/relative-redirect/4")
Returns Options self with automatically following redirects.
# Automatically following redirects.
Halite.follow
.get("http://httpbin.org/relative-redirect/5")
# Always redirect with any request methods
Halite.follow(strict: false)
.get("http://httpbin.org/get")
Get a resource
Halite.get("http://httpbin.org/anything", params: {
first_name: "foo",
last_name: "bar"
})
Get a streaming resource
Halite.get("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Head a resource
Halite.head("http://httpbin.org/anything", params: {
first_name: "foo",
last_name: "bar"
})
Head a streaming resource
Halite.head("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Make a request with the given headers
Halite.headers({"Content-Type", "application/json", "Connection": "keep-alive"})
.get("http://httpbin.org/get")
# Or
Halite.headers({content_type: "application/json", connection: "keep-alive"})
.get("http://httpbin.org/get")
Make a request with the given headers
Halite.headers(content_type: "application/json", connection: "keep-alive")
.get("http://httpbin.org/get")
Returns Options self with enable or disable logging.
Enable logging
Same as call logging method without any argument.
Halite.logging.get("http://httpbin.org/get")
Disable logging
Halite.logging(false).get("http://httpbin.org/get")
Returns Options self with given the logging which it integration from Halite::Logging.
Simple logging
Halite.logging
.get("http://httpbin.org/get", params: {name: "foobar"})
=> 2018-08-28 14:33:19 +08:00 | request | POST | http://httpbin.org/post
=> 2018-08-28 14:33:21 +08:00 | response | 200 | http://httpbin.org/post | 1.61s | application/json
{ ... }
Logger configuration
By default, Halite will logging all outgoing HTTP requests and their responses(without binary stream) to STDOUT on DEBUG level.
You can configuring the following options:
skip_request_body: By default isfalse.skip_response_body: By default isfalse.skip_benchmark: Display elapsed time, by default isfalse.colorize: Enable colorize in terminal, only apply incommonformat, by default istrue.
Halite.logging(skip_request_body: true, skip_response_body: true)
.post("http://httpbin.org/get", form: {image: File.open("halite-logo.png")})
# => 2018-08-28 14:33:19 +08:00 | request | POST | http://httpbin.org/post
# => 2018-08-28 14:33:21 +08:00 | response | 200 | http://httpbin.org/post | 1.61s | application/json
Use custom logging
Creating the custom logging by integration Halite::Logging::Abstract abstract class.
Here has two methods must be implement: #request and #response.
class CustomLogger < Halite::Logging::Abstract
def request(request)
@logger.info "| >> | %s | %s %s" % [request.verb, request.uri, request.body]
end
def response(response)
@logger.info "| << | %s | %s %s" % [response.status_code, response.uri, response.content_type]
end
end
# Add to adapter list (optional)
Halite::Logging.register_adapter "custom", CustomLogger.new
Halite.logging(logging: CustomLogger.new)
.get("http://httpbin.org/get", params: {name: "foobar"})
# We can also call it use format name if you added it.
Halite.logging(format: "custom")
.get("http://httpbin.org/get", params: {name: "foobar"})
# => 2017-12-13 16:40:13 +08:00 | >> | GET | http://httpbin.org/get?name=foobar
# => 2017-12-13 16:40:15 +08:00 | << | 200 | http://httpbin.org/get?name=foobar application/json
Returns Options self with given the file with the path.
JSON-formatted logging
Halite.logging(format: "json")
.get("http://httpbin.org/get", params: {name: "foobar"})
create a http request and log to file
Log.setup("halite.file", backend: Log::IOBackend.new(File.open("/tmp/halite.log", "a")))
Halite.logging(for: "halite.file")
.get("http://httpbin.org/get", params: {name: "foobar"})
Always create new log file and store data to JSON formatted
Log.setup("halite.file", backend: Log::IOBackend.new(File.open("/tmp/halite.log", "w"))
Halite.logging(for: "halite.file", format: "json")
.get("http://httpbin.org/get", params: {name: "foobar"})
Check the log file content: /tmp/halite.log
Options a resource
Request with form data
Halite.options("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.options("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.options("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Options a streaming resource
Halite.options("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Patch a resource
Request with form data
Halite.patch("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.patch("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.patch("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Patch a streaming resource
Halite.patch("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Post a resource
Request with form data
Halite.post("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.post("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.post("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Post a streaming resource
Halite.post("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Put a resource
Request with form data
Halite.put("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.put("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.put("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Put a streaming resource
Halite.put("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Make an HTTP request with the given verb and options
This method will be executed with oneshot request.
Halite.request("get", "http://httpbin.org/get", Halite::Options.new(
"headers" = { "user_agent" => "halite" },
"params" => { "nickname" => "foo" },
"form" => { "username" => "bar" },
)
Make an HTTP request with the given verb
Halite.request("get", "http://httpbin.org/get", {
"headers" = { "user_agent" => "halite" },
"params" => { "nickname" => "foo" },
"form" => { "username" => "bar" },
})
Make an HTTP request with the given verb and options
This method will be executed with oneshot request.
Halite.request("get", "http://httpbin.org/stream/3") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Make an HTTP request with the given verb and options
This method will be executed with oneshot request.
Halite.request("get", "http://httpbin.org/stream/3", headers: {"user-agent" => "halite"}) do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Adds a timeout to the request.
How long to wait for the server to send data before giving up, as a int, float or time span. The timeout value will be applied to both the connect and the read timeouts.
Halite.timeout(3, 3.minutes, 5)
.post("http://httpbin.org/post", form: {file: "file.txt"})
# Or
Halite.timeout(3.04, 64, 10.0)
.get("http://httpbin.org/get")
Turn on given the name of features.
Available features to review all subclasses of Halite::Feature.
Halite.use("logging", "your-custom-feature-name")
.get("http://httpbin.org/get", params: {name: "foobar"})
Turn on given features and its options.
Available features to review all subclasses of Halite::Feature.
Use JSON logging
Halite.use("logging", format: "json")
.get("http://httpbin.org/get", params: {name: "foobar"})
# => { ... }
Use common format logging and skip response body
Halite.use("logging", format: "common", skip_response_body: true)
.get("http://httpbin.org/get", params: {name: "foobar"})
# => 2018-08-28 14:58:26 +08:00 | request | GET | http://httpbin.org/get
# => 2018-08-28 14:58:27 +08:00 | response | 200 | http://httpbin.org/get | 615.8ms | application/json
Set requests user agent
Halite.user_agent("Custom User Agent")
.get("http://httpbin.org/get")