使用XHR和分块传输编码的HTTP POST [英] HTTP POST using XHR with Chunked Transfer Encoding

查看:660
本文介绍了使用XHR和分块传输编码的HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST API,它通过HTTP Post接受音频文件。 API支持Transfer-Encoding:chunked请求标头,以便在从客户端上运行的记录器创建文件时,可以将文件分段上传。这样,服务器可以在文件到达时开始处理文件,以提高性能。例如:

I have a REST API that accepts an Audio file via an HTTP Post. The API has support for Transfer-Encoding: chunked request header so that the file can be uploaded in pieces as it is being created from a recorder running on the client. This way the server can start processing the file as it arrives for improved performance. For example:


HTTP 1.1 POST ... / v1 / processAudio

HTTP 1.1 POST .../v1/processAudio

Transfer-Encoding:chunked

Transfer-Encoding: chunked

[Chunk 1 256 Bytes](服务器在到达时开始处理)

[Chunk 1 256 Bytes] (server starts processing when arrives)

[Chunk 2 256字节]

[Chunk 2 256 Bytes]

[块3 256字节]

[Chunk 3 256 Bytes]

...

音频文件通常很短,大小约为10K到100K。我有C#和Java代码正在工作,所以我知道API工作。但是,我似乎无法使用javascript在浏览器中进行录制和上传。

The audio files are typically short and are around 10K to 100K in size. I have C# and Java code that is working so I know that API works. However, I cannot seem to get the recording and upload working in a browser using javascript.

这是我的测试代码,使用Transfer-Encoding对localhost执行POST:

Here is my Test Code that does a POST to localhost with Transfer-Encoding:

<html>
<script type="text/javascript">
  function streamUpload() {
    var blob = new Blob(['GmnQPBU+nyRGER4JPAW4DjDQC19D']);
    var xhr = new XMLHttpRequest();
    // Add any 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>

问题是我在Chrome中收到以下错误

The problem is that i'm receiving the following Error in Chrome

拒绝设置不安全标头转移-Encoding

Refused to set unsafe header "Transfer-Encoding"

streamUpload @ uploadTest.html:14
onclick @ uploadTest.html:24

streamUpload @ uploadTest.html:14 onclick @ uploadTest.html:24

在查看XHR文档后,我仍然感到困惑,因为它没有谈论不安全的请求标头。我想知道XHR是否可能不允许或实现 Transfer-Encoding:chunked 用于HTTP POST?

After looking at XHR documentation i'm still confused because it does not talk about unsafe request headers. I'm wondering if its possible that XHR does not allow or implement Transfer-Encoding: chunked for HTTP POST?

我看过使用多个XHR.send()请求和WebSockets的工作但两者都不合需要,因为它需要对已经存在的服务器API进行重大更改地方,简单,稳定和工作。唯一的问题是我们似乎无法使用psedo-streaming通过Transfer-Encoding从浏览器发布POST:chunked request header。

I've looked at work arounds using multiple XHR.send() requests and WebSockets but both are undesirable because it will require significant changes to the server APIs which are already in place, simple, stable and working. The only issue is that we cannot seem to POST from a browser with psedo-streaming via Transfer-Encoding: chunked request header.

任何想法或建议都会非常有用。

Any thoughts or advice would be very helpful.

推荐答案

正如评论中所提到的,您不能设置由用户代理控制的标头。

As was mentioned in a comment, you're not allowed to set that header as it's controlled by the user agent.

有关完整的标题集,请参阅 4.6.2来自W3C XMLHttpRequest Level 1的setRequestHeader()方法并注意 Transfer-Encoding 是其中一个标题由用户代理控制,让它控制运输的这些方面。

For the full set of headers, see 4.6.2 The setRequestHeader() method from W3C XMLHttpRequest Level 1 and note that Transfer-Encoding is one of the headers that are controlled by the user agent to let it control those aspects of transport.


  • Accept-Charset

  • Accept-Encoding

  • 访问控制请求标题

  • 访问控制请求方法

  • 连接

  • 内容长度

  • Cookie

  • Cookie2

  • 日期

  • DNT

  • 期望

  • 主持人

  • 保持 - Alive

  • Origin

  • Referer

  • TE

  • 预告片

  • 转移编码

  • 升级

  • 用户代理

  • 通过

  • 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

WhatWG Fetch API生活标准中有类似的列表d。
https://fetch.spec.whatwg.org/#terminology-headers

There is a similar list in the WhatWG Fetch API Living Standard. https://fetch.spec.whatwg.org/#terminology-headers

这篇关于使用XHR和分块传输编码的HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆