HttpRes response object

Every time an HttpCli verb method is called, it will produce an HttpRes object as a result.

Let’s consider the following simple script for example:

{
  var hc = new HttpCli();
  var res = hc.Url("https://www.example.com").Timeout(30).Get();
  // "res" here above is an HttpRes object with its own methods
}

Following the example here above, the res object returned by the Get() call will have the following methods:

res.IsValid(): boolean

This method simply returns true if the http/https call was completed, or false otherwise (for example if the call times out).

res.StatusCode(): number

This method returns the status code resulting from the http/https call, for example if everything went well a .Get() request will probably return 200, while a .Post() request will likely return 201. Other common and well-known codes are 403 (unauthorized), 404 (not found), and 500 (internal server error). Learn more about HTTP status codes.

res.BodyAsString(): string

This method returns the body of the response as a string (useful when the body is a web page or a JSON object for example).

res.BodyAsBytes(): []number

This method returns the body of the response as an array of bytes (useful when the body is a binary object, like an image for example).

res.BodySaveToFile(filePath: string): boolean

This method saves the body of the response to a file which fully-qualified path is passed as an argument (ex: /downloads/budget.csv).

res.ContentType(): string

This method returns a string containing the MIME Content-Type as reported by the server.

res.ContentLength(): number

This method returns the Content-Length as reported by the server (some servers and CDNs fail to report this).

res.Encoding(): string

This method returns the Content-Transfer-Encoding as reported by the server (this also may be missing in some cases).

res.Headers(): object

This method returns a single JSON object in which each property represents one response header. Example:

{
  "Cache-Control": "max-age=604800",
  "Content-Type": "text/html; charset=UTF-8",
  "Date": "Sun, 30 Aug 2020 16:24:06 GMT",
  "X-Cache": "HIT",
  "Age": "512004",
  "Etag": "\"3147526947+gzip\"",
  "Expires": "Sun, 06 Sep 2020 16:24:06 GMT",
  "Server": "ECS (sjc/4E76)",
  "Last-Modified": "Thu, 17 Oct 2019 07:18:26 GMT",
  "Vary": "Accept-Encoding"
}
res.Cookies(): []object

This method returns an array of JSON object, in which each object represents a cookie returned by the web server in this response. For example, Google returns an array of cookies like this one:

[
  {
    "Name": "1P_JAR",
    "Value": "2020-08-30-16",
    "Path": "/",
    "Domain": ".google.com",
    "Expires": "2020-09-29T16:32:11Z",
    "RawExpires": "Tue, 29-Sep-2020 16:32:11 GMT",
    "MaxAge": 0,
    "Secure": true,
    "HttpOnly": false,
    "SameSite": 0,
    "Raw": "1P_JAR=2020-08-30-16; expires=Tue, 29-Sep-2020 16:32:11 GMT; path=/; domain=.google.com; Secure",
    "Unparsed": []
  },
  {
    "Name": "NID",
    "Value": "204=EHXBqKVUuskC5fcv3UnbLPB7oN0LU-nTODKDhuvBF5WzonZJToiwoBK12N7gr3Pycq8jCZDNS6PW9SV57GtIeCYw488",
    "Path": "/",
    "Domain": ".google.com",
    "Expires": "2021-03-01T16:32:11Z",
    "RawExpires": "Mon, 01-Mar-2021 16:32:11 GMT",
    "MaxAge": 0,
    "Secure": false,
    "HttpOnly": true,
    "SameSite": 0,
    "Raw": "NID=204=EHXBqKVUuskC5fcv3UnbLPB7oN0LU-nTODKDhuvBF5WzonZJToiwoBK12N7gr3Pycq8jCZDNS6PW9SV57GtIeCYw488; expires=Mon, 01-Mar-2021 16:32:11 GMT; path=/; domain=.google.com; HttpOnly",
    "Unparsed": []
  }
]