1

I am working on a POST request and have been using this answer. There is a lot of documentation NSUrlRequest (and connection) but I am having trouble figuring out why the request won't work.

  1. I have performed a successful POST using an HTTP Dev Client using this code

    entry.0.single=name&entry.1.single=location&entry.4.single=phoneNumber&entry.2.single=order????&pageNumber=0&backupCache=
    
  2. The 4 variables (name, location, phoneNumber, order) are all linked to textFields in the app.

    - (IBAction)placeOrder:(id)sender {
        NSURL *nsURL = [[NSURL alloc] initWithString:@"url"];
        NSMutableURLRequest *nsMutableURLRequest = [[NSMutableURLRequest alloc] initWithURL:nsURL];
    
        // Set HTTP method to POST
        [nsMutableURLRequest setHTTPMethod:@"POST"];
    
        // Set up the parameters to send.
        NSString *paramDataString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&pageNumber=0&backupCache=",@"entry.0.single", _name, @"entry.1.single", _location, @"entry.4.single", _phoneNumber, @"entry.2.single", _order];
    
        // Encode the parameters to default for NSMutableURLRequest.
        NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
    
        // Set the NSMutableURLRequest body data.
        [nsMutableURLRequest setHTTPBody: paramData];
    
    
        // Create NSURLConnection and start the request.
        NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]initWithRequest:nsMutableURLRequest delegate:self];
    
        [ nsUrlConnection start];
    
    }
    

I think I might be missing something subtle but I have been pouring through stackoverflow and developer documentation. Any thoughts would be much appreciated. Thanks

Community
  • 1
  • 1
Matt Perejda
  • 509
  • 5
  • 14
  • Make sure your controller implements the NSURLConnectionDelegate, and look at what's coming back (if anything) in connection:didReceiveResponse – Kyle Truscott Jul 19 '13 at 14:15
  • Depending on whether or not you care about the data response you'd also want to implement `NSURLConnectionDataDelegate` – Dan Jul 19 '13 at 14:37
  • I implemented the NSURLConnectionDelegate in the .h file like so: interface HelloWorldViewController : UIViewController However, I get an error stating that 'connection' is an undeclared identifier in the .m file for the 'connection:didReceiveResponse' code. Any thoughts? Thanks – Matt Perejda Jul 22 '13 at 16:55

2 Answers2

1

You would need to implement the NSURLConnectionDelegate protocol, put [nsUrlConnection setDelegate:self]; into your code and add the -connectionDidFinishLoading:, -connection:didReceiveData: and -connectionDidFailWithError: methods into your code and capture the response data:

.h
NSMutableData *responseData;

.m
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"RESPONSE: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"CONNECTION ERROR: %@", [error localizedDescription]);
}
Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
0

I did this task by using Swift. Check it out in this repo: https://github.com/goktugyil/QorumLogs

Here is the tutorial of how to set it up: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

Heres the code to do it:

private static func sendError(#text: String) {
    var url = NSURL(string: formURL)
    var postData = formField1 + "=" + text
    postData += "&" + formField2 + "=" + "anothertext"                
    postData += "&" + formField3 + "=" + "anothertext" 
    postData += "&" + formField4 + "=" + "anothertext" 

    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)
}
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
  • I'm no sure if thats possible – Esqarrouth May 16 '17 at 11:56
  • @Esqarrouth...its possible...but how to do not getting...here is the link for that...https://developers.google.com/sheets/api/quickstart/ios but How to do not getting – Priya May 17 '17 at 05:21