I have to send base64
string request to server .
Code:
NSString *body = [NSString stringWithFormat:@"[\"Login\",{\"password\":\"%@\",\"username\":\"%@\",\"ip\":\"%@\",\"login_type\":\"IOS\",\"short_name\":null}]",self.mPassword.text,self.mUsername.text,[Settings getIPAddress]];
NSLog(@"login %@",body);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[Settings getLoginUrl]]];
NSData *postData = [body dataUsingEncoding:NSDataBase64Encoding64CharacterLineLength allowLossyConversion:YES];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
But it sends an empty string to server. why so? I wish to send base64
string as a request to server.
UPDATE:
NSString *body = [NSString stringWithFormat:@"[\"Login\",{\"password\":\"%@\",\"username\":\"%@\",\"ip\":\"%@\",\"login_type\":\"IOS\",\"short_name\":null}]",self.mPassword.text,self.mUsername.text,[Settings getIPAddress]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[Settings getLoginUrl]]];
NSLog(@"%@",[Settings getLoginUrl]);
NSData *postData = [body dataUsingEncoding: NSUTF8StringEncoding];
NSString *base64EncodedStr = [postData base64EncodedStringWithOptions:0];
NSData *base64EncodedData = [[NSData alloc] initWithBase64EncodedString:base64EncodedStr options:0];
[request setHTTPBody:base64EncodedData];
[request setHTTPMethod:@"POST"];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[base64EncodedData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *connectionError)
{
if ([data length] > 0 && connectionError == nil)
{
[self receivedLoginData:data];
}
else if ([data length] == 0 && connectionError == nil)
{
[self emptyReply];
}
else if (connectionError != nil && connectionError.code == NSURLErrorTimedOut)
{
[self timedOut];
}
else if (connectionError != nil)
{
[self downloadError:connectionError];
}
}]resume];