3

Hi Please help me how to prepare NSMutableURLRequest for below api

URL : www.XXXXXXXX.com/api.php

For Login :-

www.XXXXXXXXXXXX.com/api.php?task=login

POST Data :-

"email" => User's email

"pw" => User's Password

json response: session id on successful login

Am trying like this.

NSMutableURLRequest *request;

request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"www.XXXXXXXX.com/api.php?task=login"]];


[request setHTTPMethod:@"POST"];


NSString *postString =@"email=xxxxxxxx@gmail.com&pw=1234";

[request setValue:[NSString
                   stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[postString
                      dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60
Enmud
  • 83
  • 1
  • 1
  • 9

3 Answers3

5

One reason could be that you forgot to add the "http:" scheme:

[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.XXXXXXXX.com/api.php?task=login]];
                                                     HERE --^

Note also that the correct way to set body data and in particular the length is

NSString *postString =@"email=xxxxxxxx@gmail.com&pw=1234";
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]]
              forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:postData];

because the length of the UTF-8 encoded data can be different from the (Unicode) string length.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

I ran into this error when I specified my base URL with a variable that accidentally contained a new line.

Andrew
  • 227,796
  • 193
  • 515
  • 708
-1

Try this:

NSError *error;
NSString *jsonString;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonData1
                                                   options:0 error:&error];

if (!jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString= [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

NSData *postData = [[[NSString alloc] initWithFormat:@"method=methodName&email=%@&password=%@", user_name, pass_word] dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%ld",[postData length]];

jsonData=[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"\"Accept\""];
[request setValue:@"application/json" forHTTPHeaderField:@"\"Content-Type\""];
[request setValue:postLength forHTTPHeaderField:@"\"Content-Length\""];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSError *requestError = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                                        returningResponse:&response
                                                    error:&requestError];

if ([response statusCode] >= 200 && [response statusCode] < 300) {
    NSError *serializeError = nil;

    NSString* newStr = [NSString stringWithUTF8String:[urlData bytes]];

    NSDictionary *jsonData = [NSJSONSerialization
                              JSONObjectWithData:urlData
                              options:NSJSONReadingAllowFragments
                              error:&serializeError];
     NSLog(@"recdata %@",jsonData);
}
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection)
{
    NSLog(@"theConnection is succesful");
}
[connection start];
VJVJ
  • 435
  • 3
  • 21