Skip to content

Commit 032f80e

Browse files
committed
Docs for new http API
1 parent f465949 commit 032f80e

File tree

1 file changed

+79
-70
lines changed

1 file changed

+79
-70
lines changed

doc/api/http.markdown

+79-70
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ HTTP API is very low-level. It deals with stream handling and message
2222
parsing only. It parses a message into headers and body but it does not
2323
parse the actual headers or the body.
2424

25-
HTTPS is supported if OpenSSL is available on the underlying platform.
2625

2726
## http.Server
2827

@@ -311,34 +310,57 @@ If `data` is specified, it is equivalent to calling `response.write(data, encodi
311310
followed by `response.end()`.
312311

313312

314-
## http.Client
313+
## http.request(options, callback)
315314

316-
An HTTP client is constructed with a server address as its
317-
argument, the returned handle is then used to issue one or more
318-
requests. Depending on the server connected to, the client might
319-
pipeline the requests or reestablish the stream after each
320-
stream. _Currently the implementation does not pipeline requests._
315+
Node maintains several connections per server to make HTTP requests.
316+
This function allows one to transparently issue requests.
321317

322-
Example of connecting to `google.com`:
318+
Options:
323319

324-
var http = require('http');
325-
var google = http.createClient(80, 'www.google.com');
326-
var request = google.request('GET', '/',
327-
{'host': 'www.google.com'});
328-
request.end();
329-
request.on('response', function (response) {
330-
console.log('STATUS: ' + response.statusCode);
331-
console.log('HEADERS: ' + JSON.stringify(response.headers));
332-
response.setEncoding('utf8');
333-
response.on('data', function (chunk) {
320+
- `host`: A domain name or IP address of the server to issue the request to.
321+
- `port`: Port of remote server.
322+
- `method`: A string specifing the HTTP request method. Possible values:
323+
`'GET'` (default), `'POST'`, `'PUT'`, and `'DELETE'`.
324+
- `path`: Request path. Should include query string and fragments if any.
325+
E.G. `'/index.html?page=12'`
326+
- `headers`: An object containing request headers.
327+
328+
`http.request()` returns an instance of the `http.ClientRequest`
329+
class. The `ClientRequest` instance is a writable stream. If one needs to
330+
upload a file with a POST request, then write to the `ClientRequest` object.
331+
332+
Example:
333+
334+
var options = {
335+
host: 'www.google.com',
336+
port: 80,
337+
path: '/upload',
338+
method: 'POST'
339+
};
340+
341+
var req = http.request(options, function(res) {
342+
console.log('STATUS: ' + res.statusCode);
343+
console.log('HEADERS: ' + JSON.stringify(res.headers));
344+
res.setEncoding('utf8');
345+
res.on('data', function (chunk) {
334346
console.log('BODY: ' + chunk);
335347
});
336348
});
337349

338-
There are a few special headers that should be noted.
350+
// write data to request body
351+
req.write('data\n');
352+
req.write('data\n');
353+
req.end();
354+
355+
Note that in the example `req.end()` was called. With `http.request()` one
356+
must always call `req.end()` to signify that you're done with the request -
357+
even if there is no data being written to the request body.
339358

340-
* The 'Host' header is not added by Node, and is usually required by
341-
website.
359+
If any error is encountered during the request (be that with DNS resolution,
360+
TCP level errors, or actual HTTP parse errors) an `'error'` event is emitted
361+
on the returned request object.
362+
363+
There are a few special headers that should be noted.
342364

343365
* Sending a 'Connection: keep-alive' will notify Node that the connection to
344366
the server should be persisted until the next request.
@@ -350,6 +372,33 @@ There are a few special headers that should be noted.
350372
and listen for the `continue` event. See RFC2616 Section 8.2.3 for more
351373
information.
352374

375+
## http.get(options, callback)
376+
377+
Since most requests are GET requests without bodies, Node provides this
378+
convience method. The only difference between this method and `http.request()` is
379+
that it sets the method to GET and calls `req.end()` automatically.
380+
381+
Example:
382+
383+
var options = {
384+
host: 'www.google.com',
385+
port: 80,
386+
path: '/index.html'
387+
};
388+
389+
http.get(options, function(res) {
390+
console.log("Got response: " + res.statusCode);
391+
}).on('error', function(e) {
392+
console.log("Got error: " + e.message);
393+
});
394+
395+
396+
## http.Agent
397+
398+
`http.request()` uses a special `Agent` for managing multiple connections to
399+
an HTTP server. Normally `Agent` instances should not be exposed to user
400+
code, however in certain situations it's useful to check the status of the
401+
agent.
353402

354403
### Event: 'upgrade'
355404

@@ -369,56 +418,24 @@ Emitted when the server sends a '100 Continue' HTTP response, usually because
369418
the request contained 'Expect: 100-continue'. This is an instruction that
370419
the client should send the request body.
371420

421+
### agent.maxSockets
372422

373-
### http.createClient(port, host='localhost', secure=false, [credentials])
374-
375-
Constructs a new HTTP client. `port` and
376-
`host` refer to the server to be connected to. A
377-
stream is not established until a request is issued.
378-
379-
`secure` is an optional boolean flag to enable https support and `credentials` is an optional
380-
credentials object from the crypto module, which may hold the client's private key,
381-
certificate, and a list of trusted CA certificates.
382-
383-
If the connection is secure, but no explicit CA certificates are passed
384-
in the credentials, then node.js will default to the publicly trusted list
385-
of CA certificates, as given in <http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt>.
423+
By default set to 5. Determines how many concurrent sockets the agent can have open.
386424

387-
### client.request(method='GET', path, [request_headers])
425+
### agent.sockets
388426

389-
Issues a request; if necessary establishes stream. Returns a `http.ClientRequest` instance.
427+
An array of sockets currently inuse by the Agent. Do not modify.
390428

391-
`method` is optional and defaults to 'GET' if omitted.
429+
### agent.queue
392430

393-
`request_headers` is optional.
394-
Additional request headers might be added internally
395-
by Node. Returns a `ClientRequest` object.
431+
A queue of requests waiting to be sent to sockets.
396432

397-
Do remember to include the `Content-Length` header if you
398-
plan on sending a body. If you plan on streaming the body, perhaps
399-
set `Transfer-Encoding: chunked`.
400-
401-
*NOTE*: the request is not complete. This method only sends the header of
402-
the request. One needs to call `request.end()` to finalize the request and
403-
retrieve the response. (This sounds convoluted but it provides a chance for
404-
the user to stream a body to the server with `request.write()`.)
405-
406-
### client.verifyPeer()
407-
408-
Returns true or false depending on the validity of the server's certificate
409-
in the context of the defined or default list of trusted CA certificates.
410-
411-
### client.getPeerCertificate()
412-
413-
Returns a JSON structure detailing the server's certificate, containing a dictionary
414-
with keys for the certificate `'subject'`, `'issuer'`, `'valid_from'` and `'valid_to'`.
415433

416434

417435
## http.ClientRequest
418436

419-
This object is created internally and returned from the `request()` method
420-
of a `http.Client`. It represents an _in-progress_ request whose header has
421-
already been sent.
437+
This object is created internally and returned from `http.request()`. It
438+
represents an _in-progress_ request whose header has already been sent.
422439

423440
To get the response, add a listener for `'response'` to the request object.
424441
`'response'` will be emitted from the request object when the response
@@ -488,7 +505,7 @@ followed by `request.end()`.
488505

489506
## http.ClientResponse
490507

491-
This object is created when making a request with `http.Client`. It is
508+
This object is created when making a request with `http.request()`. It is
492509
passed to the `'response'` event of the request object.
493510

494511
The response implements the `Readable Stream` interface.
@@ -499,10 +516,6 @@ The response implements the `Readable Stream` interface.
499516

500517
Emitted when a piece of the message body is received.
501518

502-
Example: A chunk of the body is given as the single
503-
argument. The transfer-encoding has been decoded. The
504-
body chunk a String. The body encoding is set with
505-
`response.setBodyEncoding()`.
506519

507520
### Event: 'end'
508521

@@ -542,7 +555,3 @@ Pauses response from emitting events. Useful to throttle back a download.
542555
### response.resume()
543556

544557
Resumes a paused response.
545-
546-
### response.client
547-
548-
A reference to the `http.Client` that this response belongs to.

0 commit comments

Comments
 (0)