1

This is my code for sending a post request to a nodejs backend.

CLLocation* location = [locationManager location];
CLLocationCoordinate2D coord = [location coordinate];
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://50.63.172.74:8080/points"]];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
NSDictionary* jsonDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:coord.latitude], @"lat", [NSNumber numberWithFloat:coord.longitude], @"lng", nil];//dictionaryWithObjectsAndKeys:coord.latitude, nil]
NSString *postString;
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];

if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
   postString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];

Using express I'm getting the request.body back on the server but it looks like this:

{ '{\n  "lat" : 0.0,\n  "lng" : 0.0\n}': '' } 

and I can't access it by just saying request.body.lat since it comes back as undefined. I want the body to look like:

{ "lat":0.0, "lng":0.0}

Any idea on how to do that using express?

4

2 回答 2

4

May this help you.

Please replace 0.0 with your actual coordiantes

NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lng", nil];
        NSArray *objects = [NSArray arrayWithObjects:@"0.0",@"0.0", nil];

        NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
        NSData *jsonData ;
        NSString *jsonString;
        if([NSJSONSerialization isValidJSONObject:jsonDictionary])
        {
            jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
            jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        }


        NSString *requestString = [NSString stringWithFormat:
                                   @"http://50.63.172.74:8080/points"];

        NSURL *url = [NSURL URLWithString:requestString];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody: jsonData];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];

        NSError *errorReturned = nil;
        NSURLResponse *theResponse =[[NSURLResponse alloc]init];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];

        if (errorReturned) {
            NSLog(@"Error %@",errorReturned.description);
        }
        else
        {
            NSError *jsonParsingError = nil;
            NSMutableArray *arrDoctorInfo  = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError];

            NSLog(@"Dict %@",arrDoctorInfo);
        }
于 2013-09-02T11:57:35.320 回答
0

The problem is that after you use NSJSONSerialization to obtain a NSData object containing your JSON data, you then create postString from that data. Eliminate that unnecessary step, and just do:

[request setHTTPBody:jsonData];

And you should get the expected JSON in your server-side code.

于 2013-03-31T01:33:29.950 回答