Objective-C equivalent of curl request - iphone

I'm trying to manipulate this curl request in Objective-C:
curl -u username:password "http://www.example.com/myapi/getdata"
I've implemented the following and I'm getting a data get error Domain=kCFErrorDomainCFNetwork Code=303 with NSErrorFailingURLKey=http://www.example.com/myapi/getdata:
// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:#"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:#"%#:%#", API_USERNAME, API_PASSWORD];
// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
[request setURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I was hoping that someone could spot where I am going wrong with my attempt to manipulate the curl request and point me in the correct direction. Is there something obvious I'm missing out? The data returned from the API is in a JSON format.

I found that the best thing to do was to not attempt the authentication within the code and to put it straight in the URL itself. The working code looks as follows:
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:#"http://%#:%##www.example.com/myapi/getdata", API_USERNAME, API_PASSWORD]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

This guide seems to do what you're looking for:
http://deusty.blogspot.co.uk/2006/11/sending-http-get-and-post-from-cocoa.html
Just as an FYI, a lot of classes accept initWithData, and NSData has a method dataWithContentsOfURL, if you want to avoid setting up NSURLConnections yourself this could be an easier way of achieving what you're looking for.

Related

Facebook curl like post request using open graph

I want to create the like button in my iOS app by using
curl -X POST \
-F 'access_token=USER_ACCESS_TOKEN' \
-F 'object=OG_OBJECT_URL' \
https://graph.facebook.com/[User FB ID]/og
.likes given by Open graph api.
For it I have made post request i.e.
NSString *stringurl=[NSString stringWithFormat:#"{access_token=AAAE08AxGmwghghghOWAxMZBz6jZAaRTUYTRMakJln3VdAeUju6E6tFn2I9feY5wtGatTm2KIgXoXYFZB0pTPQt2Oj0sNaRuX3AZDZD}&{object=http://www.google.com}&https://graph.facebook.com/100002097766610/og.likes"];
NSURL *url = [NSURL URLWithString: stringurl];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setURL:url];
[request1 setHTTPMethod:#"POST"];
[request1 setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"string..:%#",string);
But I am getting empty string. May be I am making wrong url string. If any one know that how to make the curl post request. Please give me some idea.
Thanks to all.
I attached some codes below:
Body data of Http POST request should located in 'Body', So I used 'setHttpBody:' method.
NSString *userId = #"Your ID Facebook ID";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/og.likes", userId]];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setURL:url];
[request1 setHTTPMethod:#"POST"];
NSString *body=[NSString stringWithFormat:#"access_token=YOUR_APP_ACCESS_TOKEN&object=OG_OBJECT_URL"];
[request1 setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"string..:%#",string);

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?

How to send the updated data using json to server in iphone?

am using web services (JSON). from json am getting data this data loading into tableview am trying to edit this data but after edit the data how to send this updated data to server.
please any one help me?
try this will help you.. this is the post method for updating data in WS .
NSString *post =[NSString stringWithFormat:#"uid=%#&firstname=%#&lastname=%#&phone=%#&bday=%#&about_me=%#&image=%#&image_code=%#&contact_number=%#",LoginID,fname,lname,cn,bday,abtme,strimage11,c11,cn];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"YOUR LINK"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *uData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:uData encoding:NSUTF8StringEncoding];
//
NSMutableDictionary *temp = [data JSONValue];
//
NSLog(#"%#",temp);
I am assuming you have your edited data, then
Form your json. There are several libraries out there which can help you.
Know the hostname of your server.
Know which API to hit on your server.
then pass this json as POST (GET is also ok, but POST is preferred).
Process this received json on your server.
Hope this helps. Nothing much to it actually.

How to use .NET web service in iPhone, with POST method?

I am communicating with a .NET web service in an iPhone application, using the POST method, but I am not getting XML back as response. Here is my code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc"]];
NSString *api = #"/registeruser";
NSData *body = [[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text ,txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(#"url %#",url);
NSLog(#"body %#",[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text, txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPBody:body];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
NSLog(#"body len %d",[body length]);
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Please help me. Thanks in advance.
I have done that with following.
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc/webservice/%#/%#",para1,para2]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:0 forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Have you tried something smaller first? Say doing an http post locally? Or doing an http post to that server with a simple html form?
I have run into this issue myself and decided to step back and start with a simple http post to make sure my objective-c was correct first.
I used this tutorial to get me started -hope it helps

how to pass arguments to url (.php file)

http://something.php?ownerID=13&view=userAgreement
i need to pass two variables to this ..
how can i pass these i am declared ownerid to 13 in my config playlist and i am able to get it ,how to set "view = useragreement" string to view variable....
can any body show me sample code..
i am using like
NSString* termsURL = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"termsURL"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#%#%#%#", termsURL, #"?ownerID=", ownerID,#"?view=",userAgreement]];
but is not working,i am checking parsing is success or not with this url but is failing...
Code taken straight from my app. It works for what you need
NSString *post = [NSString stringWithFormat:#"ownerID=13&view=userAgreement"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
//set up the request to the website
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://something.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *result3=[[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]autorelease];