Build an HTTP Request Step by Step
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write a class RequestBuilder that sets up an HTTP request in chained steps. new RequestBuilder(url) starts a GET request to url with no headers and no body. method(name) sets the method, stored in capitals, so method("post") gives "POST". header(name, value) sets a header under its lower-case name, so a later call with the same name in any case replaces it. json(data) sets the body to JSON.stringify(data) and the "content-type" header to "application/json". Each of these three returns the builder itself, so calls chain. build() returns a new plain object { url, method, headers, body }, where headers is an object of the headers set so far and body is null if none was set. Changing the builder later never changes an object build() has already returned.
Your code must pass
- Builds a GET request with no headers or body by default
- Chains method, header and json calls
- Stores the method in capitals and header names in lower case
- Sets a JSON body with its content type
- Built requests don't change when the builder does