javascript - HTTP POST using XHR with Chunked Transfer Encoding -
i have rest api accepts audio file via http post. api has support transfer-encoding: chunked request header file can uploaded in pieces being created recorder running on client. way server can start processing file arrives improved performance. example:
http 1.1 post .../v1/processaudio
transfer-encoding: chunked
[chunk 1 256 bytes] (server starts processing when arrives)
[chunk 2 256 bytes]
[chunk 3 256 bytes]
...
the audio files typically short , around 10k 100k in size. have c# , java code working know api works. however, cannot seem recording , upload working in browser using javascript.
here test code post localhost transfer-encoding:
<html> <script type="text/javascript"> function streamupload() { var blob = new blob(['gmnqpbu+nyrger4jpaw4djdqc19d']); var xhr = new xmlhttprequest(); // add event handlers here... xhr.open('post', '/', true); xhr.setrequestheader("transfer-encoding", "chunked"); xhr.send(blob); } </script> <body> <div id='demo'>test chunked upload using xhr</div> <button onclick="streamupload()">start upload</button> </body> </html>
the problem i'm receiving following error in chrome
refused set unsafe header "transfer-encoding"
streamupload @ uploadtest.html:14 onclick @ uploadtest.html:24
after looking @ xhr documentation i'm still confused because not talk unsafe request headers. i'm wondering if possible xhr not allow or implement transfer-encoding: chunked http post?
i've looked @ work arounds using multiple xhr.send() requests , websockets both undesirable because require significant changes server apis in place, simple, stable , working. issue cannot seem post browser psedo-streaming via transfer-encoding: chunked request header.
any thoughts or advice helpful.
as mentioned in comment, you're not allowed set header it's controlled user agent.
for full set of headers, see 4.6.2 setrequestheader() method w3c xmlhttprequest level 1 , note transfer-encoding
1 of headers controlled user agent let control aspects of transport.
- accept-charset
- accept-encoding
- access-control-request-headers
- access-control-request-method
- connection
- content-length
- cookie
- cookie2
- date
- dnt
- expect
- host
- keep-alive
- origin
- referer
- te
- trailer
- transfer-encoding
- upgrade
- user-agent
- via
there similar list in whatwg fetch api living standard. https://fetch.spec.whatwg.org/#terminology-headers
Comments
Post a Comment