Skip to content

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)
Output
200 OK
true
Hello from the server
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: Int

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)
Output
404
Not Found

By 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.

ok?(): Bool

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?())
Output
true
false

url: String

The 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)
Output
https://api.example.com/hello

headers: Dict[String, String]

All 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"])
Output
abc123
a=1
b=2

text: String

The 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.

json(): Json

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())
Output
Oslo
21.5

When 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.

bytes: Bytes

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)
Output
3
Bytes[3: ff fe 00]