Skip to content

HTTP response

HTTP response · Type

A response is what mash.http gives back for every request. It holds the status, the headers and the body, and it reads the body in whichever shape you need.

Read it by property. status is the number (200), body is the text, json is the body decoded into dictionaries and lists, headers is a dictionary of header names to lists of values, and xml is the body as an XML document.

json is forgiving. It gives null when the body isn’t valid JSON, where mash.json.decode raises an error.

The standard response methods work too. getStatusCode(), getHeaderLine("Content-Type") and hasHeader("ETag") all answer. getHeaderLine is the easiest way to read one header as text.

A response prints as its body. Put it in a text template and you get the body.

Branch on the status and read a header:

set response to mash.http.get("https://api.example.com/report", {http_errors: false})
if response.status is 200
printline response.getHeaderLine("Content-Type")
printline response.body
end

Save a download to a share:

set response to mash.http.get("https://example.com/files/price-list.pdf")
set folder to mash.files.get("Sales", "Downloads")
folder.write("price-list.pdf", response.body)
  • Each read of json decodes the body again. Keep it in a variable (set data to response.json) rather than asking for response.json["x"] again and again.
  • headers values are lists. Use getHeaderLine when you want one string.

mash.http and mash.json.

NameTypeDescription
bodystring
headersarray
jsonarray
statusint
streamnullstream
xmlFluidXml
response.canWriteToStream()

No description yet.

response.getBlob() → string

No description yet.

response.getStream()

No description yet.

response.writeToStream(resource?: mixed)

No description yet.