Http Post Request With Parameters Http Post Request With Parameters json json

Http Post Request With Parameters


What are you doing here:

[[NSURLConnection alloc] initWithRequest:request delegate:self];

This line returns a NSURLConnection but you are not storing it. This is doing nothing for you.

You are clearing your data before you read it:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    response = [[NSMutableData data] retain]; // This line is clearing your data get rid of it    NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];    NSLog(@"%@",responseString);}

Edit

-(IBAction)buttonClick:(id)sender {    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.23/testService/JsonWS.asmx?op=FirmaGetir"]                                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData                                                       timeoutInterval:15];    [request setHTTPMethod:@"POST"];    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    [request setHTTPBody:[@"firID=800" dataUsingEncoding:NSUTF8StringEncoding]];    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];    [self.connection start];}#pragma NSURLConnection Delegates- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {    if (!self.receivedData){        self.receivedData = [NSMutableData data];    }    [self.receivedData appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection {     NSString *responseString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];     NSLog(@"%@",responseString);}


I suffered this problem this morning and I just figure it out now. I guess the key to your question is How to use POST method with parameter. Actually, it is quite simple.

(1) First, you should make sure your file is ready to send. Here we say it is an NSString called stringReady. We use it as a parameter in our method called postRequest (Here is not the HTTP POST parameter we want to talk about. Don't worry).

// Send JSON to server- (void) postRequest:(NSString *)stringReady{// Create a new NSMutableURLRequestNSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.xxxxxx.io/addcpd.php"]];[req setHTTPMethod:@"POST"];

(2) Now, we say it the parameter that the server wants to get is called "data", this is the way how to insert your parameter to the HTTP body.

// Add the [data] parameterNSString *bodyWithPara = [NSString stringWithFormat:@"data=%@",stringReady];

See, it's how you add a parameter when using POST method. You just simply put the parameter before the file that you want to send. If you aleary konw what your parameter then you may better to check this website:

https://www.hurl.it/

This will help you to test if you are sending files properly and it will show the response at the bottom of the website.

(3) Third, we pack our NSString to NSData and sent it to server.

// Convert the String to NSDataNSData *postData = [bodyWithPara dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];// Set the content length and http bodyNSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];[req addValue:postLength forHTTPHeaderField:@"Content-Length"];[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];[req setHTTPBody:postData];// Create an NSURLSessionNSURLSession *session = [NSURLSession sharedSession];NSURLSessionDataTask *task = [session dataTaskWithRequest:req                                        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {                                            // Do something with response data here - convert to JSON, check if error exists, etc....                                            if (!data) {                                                NSLog(@"No data returned from the sever, error occured: %@", error);                                                return;                                            }                                            NSLog(@"got the NSData fine. here it is...\n%@\n", data);                                            NSLog(@"next step, deserialising");                                            NSError *deserr;                                            NSDictionary *responseDict = [NSJSONSerialization                                                                          JSONObjectWithData:data                                                                          options:kNilOptions                                                                          error:&deserr];                                            NSLog(@"so, here's the responseDict\n\n\n%@\n\n\n", responseDict);                                        }];[task resume];}

Hope this can help somebody who gets stuck at here.