Send umlauts from Nodejs to iOS


I don't know if this is a node or a iOS problem, but when I try to send a JSON object which contains umlauts (ä,ö,ü) the set content-length seems to be wrong.

So heres my setup: My node server sends data via:

[..mongodb request..].toArray(function(err, result) {
    res.setHeader('Content-Type', 'application/json');
    var body = JSON.stringify(result);

    console.log(body);
    console.log(body.length);

    res.setHeader('charset', 'utf8');
    res.setHeader('Content-Length', body.length);
    res.end(body);
});

this yields following object:

[
{
    "_id": "51da7eb5d5f9ad77302a26c6",
    "loc": [
        53.560994,
        9.929796
    ],
    "street": "Kühnehöfe 25",
    "time": 1373273781535
},
{
    "_id": "51da7eb9d5f9ad77302a26c7",
    "loc": [
        53.561899,
        9.930203
    ],
    "street": "Kühnehöfe 17",
    "time": 1373273785156
}
]

Which has (parsed as string) a length of 215. This is also set as content length.

In my iOS project I've got following setup:

-(void)serverRequest {
    //initialize new mutable data
    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;

    //initialize url that is going to be fetched.
    NSURL *url = [NSURL URLWithString:@"http://localhost:8000/getSpots"];

    //initialize a request from url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

    //set http method
    [request setHTTPMethod:@"POST"];
    //initialize a post data    
    NSDictionary* jsonDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:coordinate.latitude], @"lat", [NSNumber numberWithFloat:coordinate.longitude], @"lng", accessToken, @"accessToken", nil];//dictionaryWithObjectsAndKeys:coord.latitude, nil]
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict
                                                   options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];

    //set request content type we MUST set this value.
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"content-type"];    

    //set post data of request
    [request setHTTPBody:jsonData];

    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connectionGetSpots = connection;

    //start the connection
    [connection start];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.receivedData appendData:data];

    NSLog(@"loading: %d, %lld", [self.receivedData length], dataSize);  // both 215 (correct)
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    dataSize = [response expectedContentLength];
    NSLog(@"dataSize: %lld", dataSize);  // is 215 (correct)
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSString *responseData = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"rD: %@, %d" ,responseData, [responseData length]);
}

The connectionDidFinishLoading function logs this:

[
{
    "_id": "51da7eb5d5f9ad77302a26c6",
    "loc": [
        53.560994,
        9.929796
    ],
    "street": "Kühnehöfe 25",
    "time": 1373273781535
},
{
    "_id": "51da7eb9d5f9ad77302a26c7",
    "loc": [
        53.561899,
        9.930203
    ],
    "street": "Kühnehöfe 17",
    "time": 13732737851
,211

As you can see, there are 4 umlauts in the json object and 4 characters are missing. If I add another location with 2 umlauts 2 more characters will be missing.

I guess somewhere the content type is set wrong, but I'm not sure what I have to do. Any help is appreciated.

Use this for correct UTF-8 lengths:

new Buffer(body).length