HttpCli http/https verbs

The HTTP(S) protocol defines the following “verbs” (that typically all web servers honor, although restrictions may apply because of security configurations): GET, POST, PUT, PATCH, DELETE, HEAD.

AFT!’s HttpCli object, therefore, has a method for each one of the above verbs, plus one extra method to allow you to send custom verbs to web servers that may be custom-built to support them. Every call to a “verb” method produces a response of type HttpRes.

.Get(): HttpRes
.Post(): HttpRes
.Put(): HttpRes
.Patch(): HttpRes
.Delete(): HttpRes
.Head(): HttpRes
.Do(customVerb: string): HttpRes

In order to make the best use of the HttpCli object, it’s important to understand the structure of its HttpRes response object.

Here’s a few examples of valid usages of HttpCli’s verb methods

Example #1 (a simple GET):

{
  var hc = new HttpCli();
  var res = hc.Url("https://www.example.com").Timeout(30).Get();
  if (res.IsValid() && (res.StatusCode() == 200)) {
    Log(res.BodyAsString());
  }
}

Example #2 (POST with JSON payload):

{
  var hc = new HttpCli();
  var res = hc.Url("https://www.some.host").Timeout(30).ReqBody('{"name":"John","age":42}').Post();
  if (res.IsValid() && (res.StatusCode() == 201)) {
    Log('Success!');
  }
}

Example #3 (custom verb):

{
  var hc = new HttpCli();
  // Let's pretend your custom web server supports a "HELLO" verb
  var res = hc.Url("https://www.your.host").Timeout(30).Do("HELLO");
  if (res.IsValid() && (res.StatusCode() == 200)) {
    Log(res.BodyAsString());
  }
}