0

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 protected]&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:@"[email protected]" forKey:@"email"]; 
[request setPostValue:@"TESTMSG" forKey:@"message"];
[request setPostValue:@"11111" forKey:@"recipient"];

Or any other way to do this?

5
  • that variable is certainly non-standard. not going to be easy for you :) Commented May 22, 2012 at 20:09
  • Since the script you're posting to is probably only checking if the variable MO is defined, I would guess that it probably doesn't matter what you set it to Commented May 22, 2012 at 20:26
  • In php code, it is checked if (isset($_REQUEST["MO"])) , so I tried not setting any value and sending just like a post variable, but it did not work. Thanks for looking into this.. Commented May 22, 2012 at 20:40
  • Since [request setPostValue:@"" forKey:@"MO"]; didn't work, have you tried [request setPostValue:@"MO" forKey:@""];? Commented May 22, 2012 at 23:00
  • Wanted to say that [request setPostValue:@"" forKey:@"MO"]; also worked, if I change the php to _POST to check for the variable. Commented May 23, 2012 at 19:04

1 Answer 1

0

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 protected]&message=TESTMSG&recipient=11111" dataUsingEncoding:NSUTF8StringEncoding]];

Good Luck


Update answer for lastest comment

NSURL* url = [NSURL URLWithString:ServerApiURL];
NSURL* urlWithParams = [NSURL URLWithString:@"?MO&[email protected]&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"];
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Jeffery.. I did try appendPostData, it did not work.. I am thinking the reason is php code. As php already exists and in php it is using (isset($_REQUEST["MO"]) to check for the variable (instead of $_POST["MO"]). Do I need some special way in objective-C code, to tell it is a request variable (not a post variable). If so, do you know how? Or I have to change the php (no other option).
Updated my answer and the very bottom.
Jeffery, I just tried to send a request to url(ggg.abc.com/example.php), but even this did not work for me. But from the safari on the mac it works. But it does not work from the safari on the device. So how to debug why it is not going to the server. This is Apache web service running on that server. I don't know if there are any restrictions accessing it from code, also in the browser settings?
there is some issue with 3G, when I turn on wifi, now i am able to send requests to (ggg.abc.com/example.php) from code and device. I tried your last post, it works only after I change the php also to $_POST["MO"]). I wished I don't have to change php.. But happy atleast it works.. Thanks for all your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.