使用angularjs $ http将content-type设置为utf-8

时间:2013-12-09 01:28:39

标签: angularjs utf-8 content-type multipartform-data

我正在将带有文件,文本等的表单上传到appengine blobstore:

$http({
    method: 'POST',
    url: the_url,
    transformRequest: formDataObject,
    data: event,
    headers: {'Content-Type': undefined}
})

请求使用以下标题成功发送:

Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryYeRxOO7y3qHNWd83

当我尝试将内容类型设置为"multipart/form-data;charset=UTF-8"时,我会失去边界并在响应中出错:Bad content type. Please use multipart.

添加UTF-8字符集的正确方法是什么?

3 个答案:

答案 0 :(得分:4)

根据RFC 1341:

As stated in the definition of the Content-Transfer-Encoding field, no encoding other than "7bit", "8bit", or "binary" is permitted for entities of type "multipart". The multipart delimiters and header fields are always 7-bit ASCII in any case, and data within the body parts can be encoded on a part-by-part basis, with Content-Transfer-Encoding fields for each appropriate body part.

因此,在这种情况下,您必须使用Content-Transfer-Encoding代替Content-Type

答案 1 :(得分:3)

我的解决方案不是改变任何东西:

事实证明使用headers: {'Content-Type': undefined}是正确的。现在一切正常(我只改变了后端)。

麻烦的是,webob忽略了编码,因此我认为编码是错误的。升级webob修复了这个问题。这很难被发现的原因是因为开发服务器默认使用新的webob,而生产默认为较旧的webob版本,因为app.yaml中没有指定新版本:

libraries:
- name: webob
  version: "1.2.3"

答案 2 :(得分:0)

必须先设置charset,如: 内容类型:多部分/格式的数据;边界= ---- WebKitFormBoundaryYeRxOO7y3qHNWd83;字符集= UTF-8