Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 294
robert king Avatar asked Mar 21 '23 16:03

robert king


2 Answers

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.

like image 86
Jeff Hubbard Avatar answered Mar 24 '23 04:03

Jeff Hubbard


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"
like image 38
robert king Avatar answered Mar 24 '23 04:03

robert king