Http.Response
Standard library struct · Nested in Http, returned by every request
An Http.Response is a server’s finished answer to a request made with Http. A program
doesn’t make one itself; each request gives one back. It holds the status, the headers, and the body,
which can be read as text, as JSON, or as raw bytes.
const response = Http.get("https://api.example.com/hello")print(response.status, response.reason)print(response.ok?())print(response.text)200 OKtrueHello from the serverAt a glance
Section titled “At a glance”| Status | |
|---|---|
status, reason |
The server’s status number and its words |
ok?() |
Whether the status is a success |
url |
The address the answer came from |
| Headers | |
|---|---|
header(name) |
One header’s value, or nothing |
headers |
All the headers |
| Body | |
|---|---|
text |
The body as text |
json() |
The body read as Json |
bytes |
The body as raw Bytes |
Status
Section titled “Status”The server’s status number, and reason, the words that go with it. 200 is "OK", 404 is
"Not Found". Neither needs parentheses.
const response = Http.get("https://api.example.com/users/404", strict: false)print(response.status)print(response.reason)404Not FoundBy default a request with a 404 or other error status raises an HttpError instead
of returning, so you only see one of these with strict: false.
Whether the status is a success, from 200 through 299.
print(Http.get("https://api.example.com/hello").ok?())print(Http.get("https://api.example.com/users/404", strict: false).ok?())truefalseThe address the answer came from. When the request was redirected, it is the final address, not the one you asked for.
print(Http.get("https://api.example.com/old").url)https://api.example.com/helloHeaders
Section titled “Headers”The value of one header, or nothing when the server didn’t send it. The name can be written in any
capitals.
const response = Http.get("https://api.example.com/headers")print(response.header("X-Request-Id"))print(response.header("x-request-id"))print(response.header("x-missing"))abc123abc123nothingAll the headers, as a dictionary whose names are all lowercase. When the server sent the same header
more than once, the values are joined with ", ", except for set-cookie, whose values are joined
with a line break.
const response = Http.get("https://api.example.com/headers")print(response.headers["x-request-id"])print(response.headers["set-cookie"])abc123a=1b=2The body as text, read as UTF-8.
Raises an HttpError when the body isn’t valid UTF-8 text, such as an image. Use
bytes for that.
The body read as JSON, for an answer whose shape is only known as it arrives. The result is a
Json value to look inside, one step at a time.
const response = Http.get("https://api.example.com/weather", query: ["city": "Oslo"])const weather = response.json()print(weather.get("city").string())print(weather.get("current").get("temperature").float())Oslo21.5When you know the shape, Json.decode can read response.text straight
into a struct of your own.
Raises a JsonError when the body isn’t JSON. Its message includes the
address the response came from.
The body as raw bytes, with no conversion to text. Ordinary compression, such as gzip, is undone first. Use it for images, downloads, and anything that isn’t text.
const response = Http.get("https://api.example.com/binary")print(response.bytes.count)print(response.bytes)3Bytes[3: ff fe 00]