Note that there are some explanatory texts on larger screens.

plurals
  1. POAFNetworking POST to REST webservice
    primarykey
    data
    text
    <p>Brief backstory, our previous developer used ASIHTTPRequest to make POST requests and retrieve data from our webservice. For reasons unknown this portion of our app stopped working. Seemed like good enough time to future proof and go with AFNetworking. REST webservice runs on the CakePHP framework. </p> <p>In short I am not receiving the request response string using AFNetworking. </p> <p>I know the webservice works because I am able to successfully post data and receive the proper response using curl: curl -d "data[Model][field0]=field0value&amp;data[Model][field1]=field1value" <a href="https://example.com/api/class/function.plist" rel="nofollow noreferrer">https://example.com/api/class/function.plist</a></p> <p>Per the previous developer's instructions I came up with the following. </p> <pre><code>#import "AFHTTPRequestOperation.h" … - (IBAction)loginButtonPressed { NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"]; [request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"]; AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]); NSLog(@"response string: %@ ", operation.responseString); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", operation.responseString); }]; [operation start]; } </code></pre> <p>output: operation hasAcceptableStatusCode: 200 response string: a blank plist file</p> <p>attempted solution 1: <a href="https://stackoverflow.com/questions/7623275/afnetworking-post-request">AFNetworking Post Request</a> the proposed solution uses a function of AFHTTPRequestOperation called operationWithRequest. However, when I attempt to use said solution I get a warning "Class method '+operationWithRequest:completion:' not found (return type defaults to 'id'"</p> <p>attempted solution 2: NSURLConnection. output: I'm able to print the success log messaged but not the response string. *update - returns blank plist. </p> <pre><code>NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; NSString *httpBodyData = @"data[User][email]=username@example.com&amp;data[User][password]=awesomepassword"; [httpBodyData dataUsingEncoding:NSUTF8StringEncoding]; [req setHTTPMethod:@"POST"]; [req setHTTPBody:[NSData dataWithContentsOfFile:httpBodyData]]; NSHTTPURLResponse __autoreleasing *response; NSError __autoreleasing *error; [NSURLConnection sendSynchronousRequest:req returningResponse:&amp;response error:&amp;error]; // *update - returns blank plist NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]; NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"responseData %@",str); if (error == nil &amp;&amp; response.statusCode == 200) { // Process response NSLog(@"success");//returns success code of 200 but blank NSLog(@"resp %@", response ); } else { // Process error NSLog(@"error"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload