set content-type to utf-8 with angularjs $http

懵懂的女人 提交于 2019-12-30 07:03:48

问题


I'm uploading a form with files, text etc to the appengine blobstore:

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

The request sends successfully with the follwing header:

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

When I try setting the content-type to "multipart/form-data;charset=UTF-8" I lose the boundary and get an error in the response: Bad content type. Please use multipart.

What is the correct way to add the UTF-8 charset?


回答1:


According to 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.

So you have to use Content-Transfer-Encoding instead of Content-Type in this case.




回答2:


My solution was not to change anything:

It turns out using headers: {'Content-Type': undefined} was correct. Everything is now working (and I only changed the backend).

Trouble was that webob was ignoring encodings and hence I thought the encodings were wrong. Upgrading webob fixed this issue. The reason this was hard to detect was because the development server was using the new webob by default whilst the production was defaulting to an older webob version since the new version wasn't specified in app.yaml:

libraries:
- name: webob
  version: "1.2.3"



回答3:


the charset must be set after all like: Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryYeRxOO7y3qHNWd83; charset=UTF-8



来源:https://stackoverflow.com/questions/20461594/set-content-type-to-utf-8-with-angularjs-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!