Pass Headers

Headers

Headers can either be part of the client’s configuration, or passed to the specific requests.

require "httpx"

http = HTTPX.headers("accept" => "text/html")
http.get("https://google.com") # sends request with "accept: text/html"
http.get("https://google.com", headers: { "accept-language" => "swahili" }) # sends the "accept-language: swahili" along with the previous "accept" header

Trailers

httpx supports settings trailers for “HTTP/2” requests and chunked “HTTP/1.1” requests.

Set them on the request directly:

http = HTTPX.headers("accept" => "text/html")
request.build_request(:get, "https://www.example.com")
request.trailers["x-request-trace-id"] = TRACE_ID
http.request(request)

Or, in case in you need to act, p.ex. once the body is fully buffered:

http = HTTPX.headers("accept" => "text/html")
request.build_request(:get, "https://www.example.com", headers: {"x-start-at" => Time.now.httpdate})
request.on(:trailers) do |req|
  req["x-done-at"] = Time.now.httpdate
end
http.request(request)

Next: Response Handling