0

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];

1 Answers1

0

NSDataBase64Encoding64CharacterLineLength is a NSDataBase64EncodingOption and not a Base64Encoding.

You need to use for example this solution

Community
  • 1
  • 1
Andrey Volobuev
  • 862
  • 8
  • 11
  • can you please explain it with reference to my code...thank you –  Jan 11 '17 at 07:41
  • You can do something like this: `NSData *postData = [body dataUsingEncoding: NSUTF8StringEncoding]; NSString *base64EncodedStr = [postData base64EncodedStringWithOptions:0]; NSData *base64EncodedData = [[NSData alloc] initWithBase64EncodedString:base64EncodedStr options:0]; [request setHTTPBody:base64EncodedData];` – Andrey Volobuev Jan 11 '17 at 07:53
  • still its empty :( –  Jan 11 '17 at 08:09
  • That is strange - for string `["Login",{"password":"mPassword","username":"mUsername","ip":"[Settings getIPAddress]","login_type":"IOS","short_name":null}]` this code gives me `Request HTTPBody <5b224c6f 67696e22....` – Andrey Volobuev Jan 11 '17 at 08:18
  • same it give me the same but when it hits the server, it is empty –  Jan 11 '17 at 08:28