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.
How it works
Section titled “How it works”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.
Common tasks
Section titled “Common tasks”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.bodyendSave 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)Watch out for
Section titled “Watch out for”- Each read of
jsondecodes the body again. Keep it in a variable (set data to response.json) rather than asking forresponse.json["x"]again and again. headersvalues are lists. UsegetHeaderLinewhen you want one string.
See also
Section titled “See also”Properties
Section titled “Properties”| Name | Type | Description |
|---|---|---|
body | string | |
headers | array | |
json | array | |
status | int | |
stream | null | stream |
xml | FluidXml |
Methods
Section titled “Methods”canWriteToStream
Section titled “canWriteToStream”response.canWriteToStream()
No description yet.
getBlob
Section titled “getBlob”response.getBlob() → string
No description yet.
getStream
Section titled “getStream”response.getStream()
No description yet.
writeToStream
Section titled “writeToStream”response.writeToStream(resource?: mixed)
No description yet.