如何使用请求向php脚本发布请求并在目标C中发布变量

I want to send a post request from objective-C code to a php script with URL

http://ggg.abc.com/example.php?MO&email=test@gmail.com&message=TESTMSG&recipient=11111

I tried to use ASIFormDataRequest as below, but how do I specify the request variable MO in the request?

NSURL* url = [NSURL URLWithString:ServerApiURL];
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];

// Add the POST fields
//[request setPostValue:@"" forKey:@"MO"];
//[request setRequestMethod:@"MO"];

[request setPostValue:@"test@gmail.com" forKey:@"email"]; 
[request setPostValue:@"TESTMSG" forKey:@"message"];
[request setPostValue:@"11111" forKey:@"recipient"];

Or any other way to do this?

As a last resort, -appendPostData: should work.

PUT requests and custom POSTs


If you want to send data via PUT, or want to send it with POST but prefer to create the POST body yourself, use appendPostData: or appendPostDataFromFile:.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:@"PUT"];

If you want to send large amounts of data and aren’t using an ASIFormDataRequest, see the information on streaming post bodies from disk below.


For you this would be:

NSURL* url = [NSURL URLWithString:ServerApiURL];
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request appendPostData:[@"MO&email=test@gmail.com&message=TESTMSG&recipient=11111" dataUsingEncoding:NSUTF8StringEncoding]];

Good Luck


Update answer for lastest comment

NSURL* url = [NSURL URLWithString:ServerApiURL];
NSURL* urlWithParams = [NSURL URLWithString:@"?MO&email=test@gmail.com&message=TESTMSG&recipient=11111" relativeToURL:url];
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:urlWithParams];
[request setDelegate:self];
// Does this request need to be a post? If so, then call -setRequestMethod:.
// NOTE: I don't know if zero length posts work.
[request setRequestMethod:@"POST"];