1

I'm trying to execute a PHP script which increments a field in a database. I have the script working and I am currently modifying the database perfectly using ASIHTTPRequest, but I feel as though I should be using a different method given that I do not need a return.

Is this what's called an HTTP POST?

    incrementOrDecrementURL = [[NSString alloc] initWithFormat:@"http://myURL/doScript.php"];

NSURL *requestURL = [[NSURL alloc] initWithString:incrementOrDecrementURL];

//The actual request
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:requestURL];
// Becoming the request delegate
//To get callbacks like requestFinished: or requestFailed:
[request setDelegate:self];    

// Start request
[request startAsynchronous]; 
Apollo
  • 8,874
  • 32
  • 104
  • 192

3 Answers3

1

form the docs: Sending a form POST with ASIFormDataRequest

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

But also note, that ASIHTTPRequest is not maintained anymore by it's original author.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

ASIHTTPRequest by default sends a GET request, not POST. It's proper that GETs don't update anything at the server, so you should use a POST: [request setRequestMethod:@"POST"]. You can just have your doScript.php return an HTTP 204 No Content response. PHP header() function. But it's good design to return at least some sort of protocol-specific status code regarding what actually happened at the server. I often just return a simple JSON response such as {"status":"OK"}.

QED
  • 9,803
  • 7
  • 50
  • 87
0

It would be a oneway request, not a POST.

But, you should never update any database with a GET request, so you should use a POST.

One way to do this is to immediately return a response, and then update the database, but the better approach, since this library is no longer maintained (http://allseeing-i.com/ASIHTTPRequest/) you may want to use NSUrlConnection and just ignore the response, and go on.

James Black
  • 41,583
  • 10
  • 86
  • 166
  • @James.Black Coming back to your approcah with the NSURLConnection...do you think this is feasible? I basically want the user to be able to press a button and have this script executed in the background..... – Apollo Apr 09 '12 at 01:06
  • @derek.lo - Yes, you can do this in the background. Look at this: http://stackoverflow.com/questions/1728631/asynchronous-request-to-the-server-from-background-thread – James Black Apr 09 '12 at 15:35