Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Chunked HTTP request from Javascript

This question shows how to read a chunked response in Javascript, but I would like to send a chunked HTTP POST from Javascript. The documentation for XMLHttpRequest doesn't mention anything. Does the answer to this question mean that it's not possible due to browser restrictions on setting the header?

like image 886
skunkwerk Avatar asked Feb 25 '16 17:02

skunkwerk


People also ask

How do I send a chunked request?

Use the WEB SEND command to send the first chunk of the message. Specify CHUNKING(CHUNKYES) to tell CICS that it is a chunk of a message. Use the FROM option to specify the first chunk of data from the body of the message. Use the FROMLENGTH option to specify the length of the chunk.

Are HTTP requests chunked?

Answer: HTTP 1.1 supports chunked encoding, which allows HTTP messages to be broken up into several parts. Chunking is most often used by the server for responses, but clients can also chunk large requests.

How do I set transfer encoding chunked?

To enable chunked transfer encoding, set the value for AspEnableChunkedEncoding to True for the site, the server, or the virtual directory that you want to enable chunked transfer encoding for: Open a command prompt.

How does transfer encoding chunked work?

In chunked transfer encoding, the data stream is divided into a series of non-overlapping "chunks". The chunks are sent out and received independently of one another. No knowledge of the data stream outside the currently-being-processed chunk is necessary for both the sender and the receiver at any given time.

What is a chunked HTML response?

A 'chunked' response means that instead of processing the whole page, generating all of the HTML and sending it to the client, we can split the HTML into 'chunks' and send one after the other, without telling the browser how big the response will be ahead of time. Why Would Anyone Want To Do This ?

What is the best way to send HTTP requests in JavaScript?

It comes with built-in support for promises and improves over the verbose syntax of the previously discussed XMLHttpRequest. As an API built with modern application and developer needs in mind, Fetch has become one of the most popular ways to send HTTP requests in Javascript today.

How do I make a GET request in Ajax?

To make an HTTP call in Ajax, you need to initialize a new XMLHttpRequest() method, specify the URL endpoint and HTTP method (in this case GET). Finally, we use the open() method to tie the HTTP method and URL endpoint together and call the send() method to fire off the request.

How to send the first chunk of HTML content to machine?

If you have scripts & stylesheets in the <head/> of your page, you can send the first chunk with the ' head ' tag HTML content to the user's machine.


3 Answers

I don't think you can with XMLHttpRequest - it's old and somewhat limited, it's really just a backwards compatibility hack for a long-gone IE ActiveX component.

However, at the bleeding edge (not in Firefox yet) you can with fetch and TransformStream. This lets you send a stream from JS.

like image 184
Keith Avatar answered Sep 26 '22 12:09

Keith


Refer to this MDN page about Transfer-Encoding. You can set the type to chunked.

You have to specify how long each chunk will be, but it allows you to send data in chunks.

like image 29
CVerica Avatar answered Sep 27 '22 12:09

CVerica


I think XMLHttpRequest is very old, I think the alternative is to use Axios which is a newer JavaScript package which helps to handle network requests. It is much more easier than fetch and ajax in JavaScript.

like image 24
doj Avatar answered Sep 26 '22 12:09

doj