azure-contrib / node-azure-search

A JavaScript client library for the Azure Search service, which works from either from Node.js or the browser
42 stars 28 forks source link

Content-length is not being set correctly #4

Closed NoelAbrahams closed 10 years ago

NoelAbrahams commented 10 years ago

Hi,

I ran into a problem when attempting to commit a document containing accent characters.

I've traced the problem down to how the Content-Length header is initialised. Setting this to payload.length works most of the time, but when there are non-English characters the length is understated, causing Azure search to reject the request as malformed JSON.

To fix this please replace headers as follows (I've also added the charset to the headers):

        var headers = {
            "Content-Type": "application/json; charset=UTF-8",
            "api-key": options.key,
            "Accept": "application/json",
            "Accept-Charset": "UTF-8",
            "Content-Length": Buffer.byteLength(payload) // fix
        };
NoelAbrahams commented 10 years ago

Also I think we need to set the response encoding:

            var result = ""
            res.setEncoding('utf8');

            res.on('data', function(chunk) {
richorama commented 10 years ago

Thanks for the bug report, I've fixed and push to npm.