Network Requests
Network Requests
Mary Prtavyan
Table of Contents
asynchronous: user can keep interacting with page while data loads
communication pattern made possible by Ajax XMLHttpRequest
Fetch
FormData Methods
● formData.append(name, value)
● formData.append(name, blob, fileName)
● formData.delete(name)
● formData.get(name)
● formData.has(name)
‘Ancient’ ways of communicating with
another server
Using forms
One way to communicate with another server was to submit a <form> there. People
submitted it into <iframe>, just to stay on the current page
Using scripts
Another trick was to use a script tag. A script could have any src, with any domain, like
<script src="http://another.com/…">. It’s possible to execute a script from any website.
Cross-Origin Requests
Safe method: GET, POST or HEAD Any other request is considered “unsafe”
Safe headers – the only allowed custom headers are:
● Accept,
● Accept-Language,
● Content-Language,
● Content-Type with the value
application/x-www-form-urlencoded,
multipart/form-data or text/plain.
CORS for safe requests
Advantages Disadvantages
● You can use the Cache API with the ● missing a built-in method to consume
request and response objects; documents
● You can perform no-cors requests, ● no way to set a timeout yet
getting a response from a server ● can't override the content-type response
that doesn't implement CORS. You header
can't access the response body ● if the content-length response header is
directly from JavaScript, but you present but not exposed, the body's total
can use it with other APIs (e.g. the length is unknown during the streaming
Cache API); ● will call the signal's abort handler even if
● Streaming responses (with XHR the the request has been completed
entire response is buffered in ● no upload progress (support for
memory, with fetch you will be able ReadableStream instances as request
to access the low-level stream). This bodies is yet to come)
isn't available yet in all browsers, ● doesn't support
but will be soon. --allow-file-access-from-files (chromium)
XMLHttpRequest
Advantages Disadvantages
● Abort a request (this now works in ● there's no way to not send cookies (apart
Firefox and Edge, as from using the non-standard mozAnon
@sideshowbarker explains in his flag or the AnonXMLHttpRequest
comment); constructor)
● Report progress. ● can't return FormData instances
● doesn't have an equivalent to fetch's
no-cors mode
● always follow redirects
Convenience measure
Regular Polling vs Long Polling
Regular Polling
Downsides
1. Messages are passed with a delay up to
10 seconds (between requests).
2. Even if there are no messages, the
server is bombed with requests every 10
seconds, even if the user switched
somewhere else or is asleep. That’s quite
a load to handle, speaking
performance-wise.
Long Polling
Long Polling: The Flow
1. A request is sent to the server.
2. The server doesn’t close the connection until it has a message to send.
3. When a message appears – the server responds to the request with it.
4. The browser makes a new request immediately.
If the connection is lost, because of, say, a network error, the browser immediately sends a new request.
WebSocket
The Connection
Websocket vs HTTP
When can a web When not to use
socket be used: it?
● REST requests (especially GET) are not suitable for large amounts of data.
● In a REST API, there is no client request language.clients do not have control over what data
server will return.
● Over-fetching of information is a waste of network and memory resources for both the client
and server.
Thank You!