Class Response
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The Response interface of the Fetch API represents the response to a request.
[Value("Response")]
public class Response
- Inheritance
-
Response
- Inherited Members
Remarks
You can create a new Response object using the Response(Union41?, ResponseInit) constructor, but you are more likely to encounter a Response object being returned as the result of another API operation—for example, a service worker RespondWith(Task<Response>), or a simple Windowfetch.
Constructors
Response()
public Response()
Response(Union41?, ResponseInit)
The Response() constructor creates a new Response object.
public Response(Union41? body = null, ResponseInit init = null)
Parameters
bodyUnion41?initResponseInit
Remarks
Properties
Headers
The headers read-only property of the
Response interface contains the Headers object associated
with the response.
[Value("headers")]
public Headers Headers { get; }
Property Value
Remarks
Ok
The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not.
[Value("ok")]
public bool Ok { get; }
Property Value
- bool
A boolean value.
Remarks
Redirected
The redirected read-only property of the Response interface indicates whether or not the response is the result of a request you made which was redirected.
[Value("redirected")]
public bool Redirected { get; }
Property Value
- bool
A boolean value which is
trueif the response indicates that your request was redirected.
Remarks
NOTE
Checking
redirectedto prevent redirects is not recommended, because by the time a response is received, the redirect has already happened, and you may have sent the request to an unintended destination, potentially sending sensitive information.
Instead, you should do the filtering when you call Windowfetch.
See the example Disallowing redirects, which shows this being done.
-Fetch API
-ServiceWorker API
-HTTP access control (CORS)
-HTTP
Status
The status read-only property of the Response interface contains the HTTP status codes of the response.
[Value("status")]
public ushort Status { get; }
Property Value
- ushort
An unsigned short number.
This is one of the HTTP response status codes.A value is0is returned for a response whose Type isopaque,opaqueredirect, orerror.
Remarks
For example, 200 for success, 404 if the resource could not be found.
StatusText
The statusText read-only property of the Response interface contains the status message corresponding to the HTTP status code in Status.
[Value("statusText")]
public string StatusText { get; }
Property Value
- string
A String containing the HTTP status message associated with the response.
The default value is "".See HTTP response status codes for a list of codes and their associated status messages.
Note that HTTP/2 does not support status messages.
Remarks
For example, this would be OK for a status code 200, Continue for 100, Not Found for 404.
Type
The type read-only property of the Response interface contains the type of the response. The type determines whether scripts are able to access the response body and headers.
[Value("type")]
public ResponseType Type { get; }
Property Value
- ResponseType
A string, which may be any of the following values:
Remarks
Url
The url read-only property of the Response interface contains the URL of the response.
The value of the url property will be the final URL obtained after any redirects.
[Value("url")]
public string Url { get; }
Property Value
- string
A string.
Remarks
Methods
Clone()
The clone() method of the Response interface creates a clone of a response object, identical in every way, but stored in a different variable.
[Value("clone")]
public Response Clone()
Returns
Remarks
Like the underlying Tee() api,
the Response.Body of a cloned Response
will signal backpressure at the rate of the faster consumer of the two bodies,
and unread data is enqueued internally on the slower consumed body
without any limit or backpressure.
Backpressure refers to the mechanism by which the streaming consumer of data
(in this case, the code that reads the body)
slows down the producer of data (such as the TCP server)
so as not to load large amounts of data in memory
that is waiting to be used by the application.
If only one cloned branch is consumed, then the entire body will be buffered in memory.
Therefore, clone() is one way to read a response twice in sequence,
but you should not use it to read very large bodies
in parallel at different speeds.
clone() throws a TypeError if the response body has already been used.
In fact, the main reason clone() exists is to allow multiple uses of body objects (when they are one-use only.)
Error()
The error() static method of the Response interface returns a new Response object associated with a network error.
[Value("error")]
public static Response Error()
Returns
- Response
A Response object.Suppose a web app has a service worker, which contains the following
fetchevent handler:With this service worker, all fetch requests from the app will pass through the service worker to the network, except for requests to fetch "salamander.jpg", which will reject. This means that the following main thread code would throw an error, and thecatchhandler will run.
Remarks
This is mainly useful when writing service workers: it enables a service worker to send a response from a ServiceWorkerGlobalScope.Fetch event handler that will cause the Windowfetch call in the main app code to reject the promise.
An error response has its Type set to error.
Json(dynamic, ResponseInit)
The json() method of the Response interface takes
a Response stream and reads it to completion. It returns a promise which
resolves with the result of parsing the body text as JSON.
[Value("json")]
public static Response Json(dynamic data, ResponseInit init = null)
Parameters
datadynamicinitResponseInit
Returns
- Response
A Promise that resolves to a JavaScript object. This object could be
anything that can be represented by JSON — an object, an array, a string, a number…
Remarks
Note that despite the method being named json(), the result is not JSON but is instead the result of taking JSON as input and parsing it to produce a JavaScript object.
-ServiceWorker API
-Cross-Origin Resource Sharing (CORS)
-HTTP
Redirect(string, ushort)
The redirect() static method of the Response interface returns a Response resulting in a redirect to the specified URL.
[Value("redirect")]
public static Response Redirect(string url, ushort status = 0)
Parameters
Returns
Remarks
NOTE
This can be used alongside the ServiceWorker API.
A controlling service worker could intercept a page's request and redirect it as desired.
This will actually lead to a real redirect if a service worker sends it upstream.