HttpClientRequest class - dart:io library (original) (raw)

HTTP request for a client connection.

To set up a request, set the headers using the headers property provided in this class and write the data to the body of the request.HttpClientRequest is an IOSink. Use the methods from IOSink, such as writeCharCode(), to write the body of the HTTP request. When one of the IOSink methods is used for the first time, the request header is sent. Calling any methods that change the header after it is sent throws an exception.

When writing string data through the IOSink the encoding used is determined from the "charset" parameter of the "Content-Type" header.

var client = HttpClient();
HttpClientRequest request = await client.get('localhost', 80, '/file.txt');
request.headers.contentType =
    ContentType('application', 'json', charset: 'utf-8');
request.write('text contentπŸ‘πŸŽ―'); // Strings written will be UTF-8 encoded.

If no charset is provided the default of ISO-8859-1 (Latin 1) is used.

var client = HttpClient();
HttpClientRequest request = await client.get('localhost', 80, '/file.txt');
request.headers.add(HttpHeaders.contentTypeHeader, "text/plain");
request.write('blΓ₯bΓ¦rgrΓΈd'); // Strings written will be ISO-8859-1 encoded

An exception is thrown if you use an unsupported encoding and thewrite() method being used takes a string parameter.

Implemented types

Properties

bufferOutput ↔ bool

Gets or sets if the HttpClientRequest should buffer output.

getter/setter pair

connectionInfo β†’ HttpConnectionInfo?

Gets information about the client connection.

no setter

contentLength ↔ int

Gets and sets the content length of the request.

getter/setter pair

cookies β†’ List<Cookie>

Cookies to present to the server (in the 'cookie' header).

no setter

done β†’ Future<HttpClientResponse>

An HttpClientResponse future that will complete once the response is available.

no setteroverride

encoding ↔ Encoding

The Encoding used when writing strings.

getter/setter pairinherited

followRedirects ↔ bool

Whether to follow redirects automatically.

getter/setter pair

hashCode β†’ int

The hash code for this object.

no setterinherited

Returns the client request headers.

no setter

maxRedirects ↔ int

Set this property to the maximum number of redirects to follow when followRedirects is true. If this number is exceeded an error event will be added with a RedirectException.

getter/setter pair

method β†’ String

The method of the request.

no setter

persistentConnection ↔ bool

The requested persistent connection state.

getter/setter pair

runtimeType β†’ Type

A representation of the runtime type of the object.

no setterinherited

uri β†’ Uri

The uri of the request.

no setter

Methods

abort([Object? exception, StackTrace? stackTrace])β†’ void

Aborts the client connection.

add(List<int> data)β†’ void

Adds byte data to the target consumer, ignoring encoding.

inherited

addError(Object error, [StackTrace? stackTrace])β†’ void

Passes the error to the target consumer as an error event.

inherited

addStream(Stream<List<int>> stream)β†’ Future

Adds all elements of the given stream.

inherited

close()β†’ Future<HttpClientResponse>

Close the request for input. Returns the value of done.

override

flush()β†’ Future

Returns a Future that completes once all buffered data is accepted by the underlying StreamConsumer.

inherited

noSuchMethod(Invocation invocation)β†’ dynamic

Invoked when a nonexistent method or property is accessed.

inherited

toString()β†’ String

A string representation of this object.

inherited

write(Object? object)β†’ void

Converts object to a String by invoking Object.toString andadds the encoding of the result to the target consumer.

inherited

writeAll(Iterable objects, [String separator = ""])β†’ void

Iterates over the given objects and writes them in sequence.

inherited

writeCharCode(int charCode)β†’ void

Writes the character of charCode.

inherited

writeln([Object? object = ""])β†’ void

Converts object to a String by invoking Object.toString and writes the result to this, followed by a newline.

inherited

Operators

operator ==(Object other)β†’ bool

The equality operator.

inherited