HTTP(S) verbs
The HTTP(S) protocol defines several standard verbs (methods) that most web servers support: GET, POST, PUT, PATCH, DELETE, HEAD. Some servers may also support custom verbs for specialized operations.
The HttpCli object in SyncJS provides a method for each standard verb, plus a .Do() method for sending custom verbs. Each verb method returns an HttpRes response object.
ts
.Get(): HttpRes
.Post(): HttpRes
.Put(): HttpRes
.Patch(): HttpRes
.Delete(): HttpRes
.Head(): HttpRes
.Do(customVerb: string): HttpResNOTE
Every call to a verb method produces a new HttpRes object. Always check the response for validity and status before using its data.
Examples
Example 1: Simple GET
ts
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
ts
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
ts
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());
}