iosjsonsthttprequest

STHTTPRequest how to postData not Key-Value?


I try using STHTTPRequest to post JSON Object:

{
 "login":"name",
 "password":"password"
}

how i can do it?

I write ios code:

STHTTPRequest * request=[STHTTPRequest requestWithURLString:@"http://moscow2013.hol.es/test.php"];
request.completionBlock=^(NSDictionary *headers, NSString *body) {
    NSLog(@"Data:%@",body);
};
request.errorBlock=^(NSError *error) {
    NSLog(@"Error:%@",error);

};

NSMutableDictionary *postData=[[NSMutableDictionary alloc] init];
NSMutableDictionary *data=[[NSMutableDictionary alloc] init];
[data setValue:@"a3bc5193-60b2-4f61-8f2d-a24b01423cb1" forKey:@"login"];
[data setValue:udid forKey:@"udid"];
[postData setValue:data forKey:@"data"];
request.encodePOSTDictionary=NO;
request.POSTDictionary=postData;
request.postDataEncoding=NSUTF8StringEncoding;
[request startAsynchronous];

But on server i got:

Array
(
    [data] => {
    login = "a3bc5193-60b2-4f61-8f2d-a24b01423cb1";
    udid = "4D6A3ABF-29CF-4D79-9716-BBBD8160626F";
}
)

How me send json as object?

I do next:

NSString *jsonString =nil;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];
if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://moscow2013.hol.es/horeca.php"]];
[request2 setHTTPMethod:@"POST"];
[request2 setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = jsonString;
NSData *data2 = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request2 setHTTPBody:data2];
[request2 setValue:[NSString stringWithFormat:@"%u", [data2 length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request2 delegate:self];
NSData *retData=    [NSURLConnection sendSynchronousRequest:request2 returningResponse:nil error:nil];

NSString *retDataStr=[[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
NSLog(@"%@",retDataStr);

And then on server side i got my correct json.


Solution

  • I added a rawPOSTData property.

    You can pull the latest version and try this:

    NSDictionary *d = @{ @"login":@"myLogin", @"udid":@"myUDID" };
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:d
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    STHTTPRequest *request = [STHTTPRequest requestWithURLString:@"http://moscow2013.hol.es/test.php"];
    
    [request setHeaderWithName:@"content-type" value:@"application/json; charset=utf-8"];
    
    request.rawPOSTData = jsonData;
    
    request.completionBlock=^(NSDictionary *headers, NSString *body) {
        NSLog(@"Body: %@", body);
    };
    
    request.errorBlock=^(NSError *error) {
        NSLog(@"Error: %@", [error localizedDescription]);
    };
    
    [request startAsynchronous];
    

    Let me know if it doesn't meet your needs.