public abstract class RequestBody extends Object
Constructor and Description |
---|
RequestBody() |
Modifier and Type | Method and Description |
---|---|
long |
contentLength()
Returns the number of bytes that will be written to
sink in a call to writeTo(okio.BufferedSink) ,
or -1 if that count is unknown. |
abstract MediaType |
contentType()
Returns the Content-Type header for this body.
|
static RequestBody |
create(MediaType contentType,
byte[] content)
Returns a new request body that transmits
content . |
static RequestBody |
create(MediaType contentType,
byte[] content,
int offset,
int byteCount)
Returns a new request body that transmits
content . |
static RequestBody |
create(MediaType contentType,
okio.ByteString content)
Returns a new request body that transmits
content . |
static RequestBody |
create(MediaType contentType,
File file)
Returns a new request body that transmits the content of
file . |
static RequestBody |
create(MediaType contentType,
String content)
Returns a new request body that transmits
content . |
boolean |
isDuplex()
A duplex request body is special in how it is transmitted on the network and
in the API contract between OkHttp and the application.
|
boolean |
isOneShot()
Returns true if this body expects at most one call to
writeTo(okio.BufferedSink) and can be transmitted
at most once. |
abstract void |
writeTo(okio.BufferedSink sink)
Writes the content of this request to
sink . |
@Nullable public abstract MediaType contentType()
public long contentLength() throws IOException
sink
in a call to writeTo(okio.BufferedSink)
,
or -1 if that count is unknown.IOException
public abstract void writeTo(okio.BufferedSink sink) throws IOException
sink
.IOException
public boolean isDuplex()
This method returns false unless it is overridden by a subclass.
With regular HTTP calls the request always completes sending before the response may begin receiving. With duplex the request and response may be interleaved! That is, request body bytes may be sent after response headers or body bytes have been received.
Though any call may be initiated as a duplex call, only web servers that are specially designed for this nonstandard interaction will use it. As of 2019-01, the only widely-used implementation of this pattern is gRPC.
Because the encoding of interleaved data is not well-defined for HTTP/1, duplex request bodies may only be used with HTTP/2. Calls to HTTP/1 servers will fail before the HTTP request is transmitted. If you cannot ensure that your client and server both support HTTP/2, do not use this feature.
Duplex APIs
With regular request bodies it is not legal to write bytes to the sink passed to writeTo(okio.BufferedSink)
after that method returns. For duplex requests bodies that condition is
lifted. Such writes occur on an application-provided thread and may occur concurrently with
reads of the ResponseBody
. For duplex request bodies, writeTo(okio.BufferedSink)
should return
quickly, possibly by handing off the provided request body to another thread to perform
writing.
public boolean isOneShot()
writeTo(okio.BufferedSink)
and can be transmitted
at most once. This is typically used when writing the request body is destructive and it is not
possible to recreate the request body after it has been sent.
This method returns false unless it is overridden by a subclass.
By default OkHttp will attempt to retransmit request bodies when the original request fails
due to a stale connection, a client timeout (HTTP 408), a satisfied authorization challenge
(HTTP 401 and 407), or a retryable server failure (HTTP 503 with a Retry-After: 0
header).
public static RequestBody create(@Nullable MediaType contentType, String content)
content
. If contentType
is non-null
and lacks a charset, this will use UTF-8.public static RequestBody create(@Nullable MediaType contentType, okio.ByteString content)
content
.public static RequestBody create(@Nullable MediaType contentType, byte[] content)
content
.public static RequestBody create(@Nullable MediaType contentType, byte[] content, int offset, int byteCount)
content
.public static RequestBody create(@Nullable MediaType contentType, File file)
file
.Copyright © 2019. All rights reserved.