Problems with using POST from iPhone to REST service - iphone

I'm building an iPhone client that will GET and POST to a REST service.
The GET-part works fine, but I don't seem to be able to POST anything. The server only accepts xml at the moment.
I've tried to use ASIHTTPRequest, ASIFormDataRequest and doing it myself but nothing seems to work.
Can anyone help me? I'm fairly new to Objective-C programming.
I do get a status code: 200 as a response using the following code in didReceiveResponse:
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
int myStatus = httpResponse.statusCode;
if (myStatus == 400) {
NSLog(#"Status code: 400");
} else if (myStatus == 200) {
NSLog(#"Status code: 200");
} else {
NSLog(#"Other status code");
Here follows three different approaches...
Code (ASIHTTPRequest):
NSURL *url = [NSURL URLWithString:#"myUrl.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[#"<my xml>" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
Code (ASIFormDataRequest):
NSURL *url = [NSURL URLWithString:#"someUrl.com"];
ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
// [theRequest setPostValue:#"" forKey:#"key1"];
[theRequest setPostValue:#"90" forKey:#"key2"];
[theRequest setPostValue:#"150" forKey:#"key3"];
// [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
[theRequest startAsynchronous];
Code (Third method):
NSData *myPostData = [[NSString stringWithFormat:#"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
// NSString *xmlPostData = #"<my xml>";
NSURL *theURL = [NSURL URLWithString:#"someUrl.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:#"application/xml; charset=utf-8" forHTTPHeaderField:#"Content-type"];
// [theRequest setValue:#"UTF-8" forHTTPHeaderField:#"Charset"];
[theRequest setHTTPBody:myPostData];// [xmlPostData dataUsingEncoding:NSUTF8StringEncoding]];// myPostData];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
How about all these -(void)connection: style functions? Does any of them have to contain specific code for the connection to work?
Many thanks for any help!

NSURL *url = [NSURL URLWithString:#"http://myURL.com"];
ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *myPostData = [[NSString stringWithFormat:#"<My XML Body>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];
[request addRequestHeader:#"Content-Type" value:#"application/xml"];
[request setPostBody:myMutablePostData];
[request setDelegate:self];
[request startSynchronous];

Related

Issuing a post request iPhone->Server from xcode

I've done many searches but none of the code fragments I find seem to work for me. I have the following fragment in xcode for running within my iPhone app:
NSURL *url = [NSURL URLWithString:#"http://blah.blah.com/rec.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSData *body = [#"data=crap" dataUsingEncoding:NSUTF8StringEncoding];
NSString *bodyLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:bodyLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:body];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
The rec.php is called fine but the post data does not make it through. The entry $_POST['data'] is empty. Could anyone let me know what I'm doing wrong?

Is it necessary to create soap message when using ASIFormDataRequest/ASIHTTPRequest?

I used NSURLConnection to communicate with Soap webservice and it worked fine.
Now I am using ASIFormDataRequest and I am not getting is it necessary to create soap message for that? and I am not aware what is SoapAction.
Here is the code I am using
NSURL *url = [NSURL URLWithString:#"http://SERVERNAME.com/WEBSERVICENAME"];
[self setFrmdataReq:[ASIFormDataRequest requestWithURL:url]];
[frmdataReq setRequestMethod:#"POST"];
[frmdataReq addRequestHeader:#"Content-Type" value:#"text/xml; charset=utf-8"];
[frmdataReq setPostValue:txtField.text forKey:#"city"];
[frmdataReq setDelegate:self];
[frmdataReq setDidFinishSelector:#selector(requestFinished:)];
[frmdataReq setDidFailSelector:#selector(requestFailed:)];
[frmdataReq startSynchronous];
UPDATE
Soap Message I used,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\
<soap12:Body>\
<getCityTime xmlns=\"http://www.Nanonull.com/TimeService/\">\
<city>%#</city>\
</getCityTime>\
</soap12:Body>\
</soap12:Envelope>",txtField.text];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapMessage length]];
[request addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn)
{
responseData = [[NSMutableData data]retain];
NSLog(#"connection successful");
}
else
NSLog(#"connection fail.");
I didn't defined SOAPAction as I don't know anything about that.
When using ASIHTTPRequest, you need to create the SOAP request in basically the same way as you do with NSURLConnection.
The setPostValue call in ASIHTTPRequest is for create HTTP POST requests in the same format use for POST <form> tags in HTML.
For comparing ASIHTTPRequest / NSURLConnection, see things like:
ASIHTTPRequest vs NSURLConnection
and "What is ASIHTTPRequest?" on:
http://allseeing-i.com/ASIHTTPRequest/

Cant Post To Server Well

i am trying to post to a server in the following code.
i cant receive the response that i need to get.
think i am making something wrong, need some help
NSString *uuid = #"acdede9b-6fa6-44d0-8837-cd11771139be";
NSString *rec = #"ewoJInNpZ25hdHVyZSIgPSAiQWphZElyM2hFclc2L0xReDYwNG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lEMGdJbU52YlM1aGJXOTBaV05vTGs5dVpWbGxZWElpT3dvSkluUnlZVzV6WVdOMGFXOXVMV2xrSWlBOUlDSXhNREF3TURBd01ERTBPRE00TURnMUlqc0tDU0p2Y21sbmFXNWhiQzF3ZFhKamFHRnpaUzFrWVhSbElpQTlJQ0l5TURFeExURXhMVEkzSURJeU9qRXpPak16SUVWMFl5OUhUVlFpT3dvSkltOXlhV2RwYm1Gc0xYUnlZVzV6WVdOMGFXOXVMV2xrSWlBOUlDSXhNREF3TURBd01ERTBPREk1TVRJeElqc0tDU0ppYVdRaUlEMGdJbU52YlM1aGJXOTBaV05vTG1Gd2NHTnBaR1Z1ZEVGd2NDSTdDZ2tpWW5aeWN5SWdQU0FpTVM0d0lqc0tmUT09IjsKCSJlbnZpcm9ubWVudCIgPSAiU2FuZGJveCI7CgkicG9kIiA9ICIxMDAiOwoJInNpZ25pbmctc3RhdHVzIiA9ICIwIjsKfQ==";
NSString *ServerContent = [NSString stringWithFormat:#"registerRecipt.aspx?udid=%#&receipt=%#",uuid,rec];
NSData *requestdata = [ServerContent dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"myserverurl"]];
request.timeoutInterval = 20;
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestdata];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
error
The page cannot be displayed Open IIS Help, which is accessible in IIS Manager (inetmgr),
Try setting the Content-Length header:
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]]
forHTTPHeaderField:#"Content-Length"];

Post Data but GET Method json -ios

how can i use the get method to post data on an api link with 6 values and a json format?
im really fuzzled. im doing this for days . is it best to have 6 textfields that will be used to write the value and a button to indicate post data?
check out my code and please help.
-(IBAction)postDataPressed
{
NSString *urlString = #"http://192.168.18.8/apisample2/friendb.php?fm=jsn";
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:user.text forKey:#"un"];
[request setPostValue:pass.text forKey:#"pd"];
[request setPostValue:gender.text forKey:#"gd"];
[request setPostValue:age.text forKey:#"ag"];
[request setPostValue:status.text forKey:#"st"];
[request setPostValue:lookfor.text forKey:#"lf"];
[request setRequestMethod:#"GET"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request startAsynchronous];
}
i hope someone will help.
NSString *request_url = [NSString stringWithFormat: #"http://192.168.18.8/apisample2/friendb.php?fm=jsn&un=%#&pd=%#&gd=%#&ag=%#&st=%#&lf=%#",
user.text, pass.text, gender.text, age.text,status.text,lookfor.text];
NSURL *url = [NSURL URLWithString:request_url];
ASIHttpRequest *request = [ASIHttpRequest requestWithURL:url];
request.delegate = self;
....
HTTP Get method only accept data in url, called query string, so you have to build query string yourself.

Problem to post large amount of data to the web server in iphone

I am working in simple application in which I need to post data to the server using webservices. I post string data to the web server, but it gives an error "large amount of binary data will be truncated".
NSString *queryString = [NSString stringWithFormat:#"url/message=%#",strMessage];
queryString = [queryString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:queryString];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:0 forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"GET"];
NSURLConnection *conn =
[[NSURLConnection alloc] initWithRequest:req delegate:self];
Any ideas?
Thanks in advance.
Did you try http://allseeing-i.com/ASIHTTPRequest/ ?
You can post any kind of datas. For example to send an image :
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"];
(from http://allseeing-i.com/ASIHTTPRequest/How-to-use)
Good Luck ! :-)