Sending an HTTP POST request on iOS

173,958

Solution 1

The following code describes a simple example using POST method.(How one can pass data by POST method)

Here, I describe how one can use of POST method.

1. Set post string with actual username and password.

NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@",@"username",@"password"]; 

2. Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

You need to send the actual length of your data. Calculate the length of the post string.

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 

3. Create a Urlrequest with all the properties like HTTP method, http header field with length of the post string. Create URLRequest object and initialize it.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

Set the Url for which your going to send the data to that request.

[request setURL:[NSURL URLWithString:@"http://www.abcde.com/xyz/login.aspx"]]; 

Now, set HTTP method (POST or GET). Write this lines as it is in your code.

[request setHTTPMethod:@"POST"]; 

Set HTTP header field with length of the post data.

[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

Also set the Encoded value for HTTP header Field.

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

Set the HTTPBody of the urlrequest with postData.

[request setHTTPBody:postData];

4. Now, create URLConnection object. Initialize it with the URLRequest.

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

It returns the initialized url connection and begins to load the data for the url request. You can check that whether you URL connection is done properly or not using just if/else statement as below.

if(conn) {
    NSLog(@"Connection Successful");
} else {
    NSLog(@"Connection could not be made");
}

5. To receive the data from the HTTP request , you can use the delegate methods provided by the URLConnection Class Reference. Delegate methods are as below.

// This method is used to receive the data which we get using post method.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data

// This method receives the error report in case of connection is not made to server. 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection

Also Refer This and This documentation for POST method.

And here is best example with source code of HTTPPost Method.

Solution 2

-(void)sendingAnHTTPPOSTRequestOniOSWithUserEmailId: (NSString *)emailId withPassword: (NSString *)password{
//Init the NSURLSession with a configuration
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

//Create an URLRequest
NSURL *url = [NSURL URLWithString:@"http://www.example.com/apis/login_api"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

//Create POST Params and add it to HTTPBody
NSString *params = [NSString stringWithFormat:@"email=%@&password=%@",emailId,password];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

//Create task
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //Handle your response here
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
     NSLog(@"%@",responseDict);
}];
   [dataTask resume];
}

Solution 3

I am not really sure why, but as soon as I comment out the following method it works:

connectionDidFinishDownloading:destinationURL:

Furthermore, I don't think you need the methods from the NSUrlConnectionDownloadDelegate protocol, only those from NSURLConnectionDataDelegate, unless you want some download information.

Solution 4

Heres the method I used in my logging library: https://github.com/goktugyil/QorumLogs

This method fills html forms inside Google Forms. Hope it helps someone using Swift.

var url = NSURL(string: urlstring)

var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)

Solution 5

Objective C

Post API with parameters and validate with url to navigate if json
response key with status:"success"

NSString *string= [NSString stringWithFormat:@"url?uname=%@&pass=%@&uname_submit=Login",self.txtUsername.text,self.txtPassword.text];
    NSLog(@"%@",string);
    NSURL *url = [NSURL URLWithString:string];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);
    NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"responseData: %@", str);
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                         options:kNilOptions
                                                           error:nil];
    NSDictionary* latestLoans = [json objectForKey:@"status"];
    NSString *str2=[NSString stringWithFormat:@"%@", latestLoans];
    NSString *str3=@"success";
    if ([str3 isEqualToString:str2 ])
    {
        [self performSegueWithIdentifier:@"move" sender:nil];
        NSLog(@"successfully.");
    }
    else
    {
        UIAlertController *alert= [UIAlertController
                                 alertControllerWithTitle:@"Try Again"
                                 message:@"Username or Password is Incorrect."
                                 preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action){
                                                       [self.view endEditing:YES];
                                                   }
                             ];
        [alert addAction:ok];
        [[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
        [self presentViewController:alert animated:YES completion:nil];
        [self.view endEditing:YES];
      }

JSON Response : {"status":"success","user_id":"58","user_name":"dilip","result":"You have been logged in successfully"} Working code

**

Share:
173,958

Related videos on Youtube

Daan Luttik
Author by

Daan Luttik

A CTO that still loves to work the full stack for data solutions. With a background in Data Science & Entrepreneurship (Msc) and Software Engineering (Bsc)

Updated on October 22, 2020

Comments

  • Daan Luttik
    Daan Luttik over 3 years

    I'm trying to send an HTTP Post with the iOS application that I'm developing but the push never reaches the server although I do get a code 200 as response (from the urlconnection). I never get a response from the server nor does the server detect my posts (the server does detect posts coming from android)

    I do use ARC but have set pd and urlConnection as strong.

    This is my code for sending the request

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                        initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",dk.baseURL,@"daantest"]]];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"text/xml"
       forHTTPHeaderField:@"Content-type"];
    
        NSString *sendString = @"<data><item>Item 1</item><item>Item 2</item></data>";
    
        [request setValue:[NSString stringWithFormat:@"%d", [sendString length]] forHTTPHeaderField:@"Content-length"];
    
        [request setHTTPBody:[sendString dataUsingEncoding:NSUTF8StringEncoding]];
        PushDelegate *pushd = [[PushDelegate alloc] init];
        pd = pushd;
        urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:pd];
        [urlConnection start];
    

    this is my code for the delegate

    #import "PushDelegate.h"
    
    @implementation PushDelegate
    @synthesize data;
    
    -(id) init
    {
        if(self = [super init])
        {
            data = [[NSMutableData alloc]init];
            [data setLength:0];
        }
        return self;
    }
    
    
    - (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten
    {
        NSLog(@"didwriteData push");
    }
    - (void)connectionDidResumeDownloading:(NSURLConnection *)connection totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes
    {
        NSLog(@"connectionDidResumeDownloading push");
    }
    
    - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
    {
        NSLog(@"didfinish push @push %@",data);
    }
    
    - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
    {
        NSLog(@"did send body");
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [self.data setLength:0];
        NSHTTPURLResponse *resp= (NSHTTPURLResponse *) response;
        NSLog(@"got response with status @push %d",[resp statusCode]);
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d
    {
        [self.data appendData:d];
    
        NSLog(@"recieved data @push %@", data);
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
    
        NSLog(@"didfinishLoading%@",responseText);
    
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error ", @"")
                                    message:[error localizedDescription]
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK", @"")
                          otherButtonTitles:nil] show];
        NSLog(@"failed &push");
    }
    
    // Handle basic authentication challenge if needed
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        NSLog(@"credentials requested");
        NSString *username = @"username";
        NSString *password = @"password";
    
        NSURLCredential *credential = [NSURLCredential credentialWithUser:username
                                                                 password:password
                                                              persistence:NSURLCredentialPersistenceForSession];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }
    
    @end
    

    The console always prints the following lines and the following lines only:

    2013-04-01 20:35:04.341 ApprenticeXM[3423:907] did send body
    2013-04-01 20:35:04.481 ApprenticeXM[3423:907] got response with status @push 200
    2013-04-01 20:35:04.484 ApprenticeXM[3423:907] didfinish push @push <>
    
  • Jelle
    Jelle about 11 years
    Also check if there is no caching going on. That would explain why you got a '200', without anything happening on the server.
  • Jelle
    Jelle about 11 years
    Check the accepted answer of stackoverflow.com/questions/405151/… and/or google for "nsurlconnection cache policy"
  • Daan Luttik
    Daan Luttik about 11 years
    I do want the download information that's why i have the function connectionDidFinishDownloading:destinationURL:
  • nickygerritsen
    nickygerritsen about 11 years
    Apparently NSURLConnectionDownloadDelegate only works in Newsstand apps.... At least that is what this thread says: stackoverflow.com/questions/6735121/…
  • Daan Luttik
    Daan Luttik about 11 years
    Thank you so much i checked so much threads and this: yuvarajmanickam.wordpress.com/2012/10/17/… just worked.
  • CouchDeveloper
    CouchDeveloper about 10 years
    Even though the answer has been accepted so many times, the given code in this answer has a number of glaring issues, and will be troublesome in practice.
  • iPatel
    iPatel about 10 years
    @CouchDeveloper - I dont know what is ur problem > ? can you explain me ?
  • iHulk
    iHulk over 9 years
    @iPatel But can we send image data using above given post method code.
  • elephant.lyh
    elephant.lyh almost 9 years
    As nowadays the popular use case is to send JSON from app to web application, please take note of a few points below: suggest to use NSUTF8StringEncoding for sending and receiving; if the post body is a json, should use [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; instead of @"application/x-www-form-urlencoded"
  • ge0rg
    ge0rg over 8 years
    Unfortunately, this code is incorrect and prone to injection attacks. If the user happens to have an "&" character in the password, all further characters will be parsed as an additional POST parameter. Deliberate manipulation is possible.
  • Harshit Goel
    Harshit Goel about 7 years
    @iPatel can you help me with this stackoverflow.com/questions/42573527/…
  • Developer
    Developer about 7 years
    What if I have to open a Safari rather than UIWebView and need to send Post Parameters to browser?
  • Daan Luttik
    Daan Luttik over 6 years
    This is new... Getting replies 4 years after your question is posted :p
  • Ronny Morán
    Ronny Morán over 6 years
    This "postexample" function is more fast that Response :p @DaanLuttik