Do a GET request with parameters on iPhone SDK? - iphone

I'm doing an app which retrieves user info, and it does that using a GET request, but I don't know how could I make one. Already tried ASIHTTPRequest with this code:
NSURL *url = [NSURL URLWithString:#"http://forrst.com/api/v2/users/info"];
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"1" forKey:#"id"];
[request setRequestMethod:#"GET"];
// Initiate the WebService request
[request setDelegate:self];
[request startAsynchronous];
But I get this response from forrst:
2011-06-05 16:59:32.189 Forrsty[4335:207] {"resp":{"error":"you must pass either id or username"},"stat":"fail","in":0.0557,"authed":false,"authed_as":false,"env":"prod"}
Which I understand I'm not doing the GET request ok, so how I would do it? Thanks :)

A Get Parameter ?
So why don't you try "http://forrst.com/api/v2/users/info?id=1" ?
[ NSString stringWithFormat:#"http://forrst.com/api/v2/users/info?id=%d", 1 ];
By the way, take a look at this librayry : http://allseeing-i.com/ASIHTTPRequest/
Good luck !

Related

AFNetworking AFMultipartFormData how to set key

I want to upload (POST) image to server with setkey "FileData" like before (wrote with ASIHTTPRequest and it works)
[self.request setData:imageData withFileName:dateFormatted andContentType:#"image/jpeg" forKey:#"Filedata"];
with AFNetworking I set like below:
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:#"POST" path:#"" parameters:paramsDic constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:#"Filedata" fileName:dateFormatted mimeType:#"image/jpeg"];
}];
seems name not actually works...
How should I set the key? #mattt
Thanks
I figure it out, the problem is I add this line
[afRequest setValue:#"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:#"Content-Type"];
But I still don't know why, I add this line to other http request and it works, but file upload. I checked server, when do file upload with this line, server received many ... hm... come to think how to describe it, mass letters ...like below:
Content-Type: image/jpeg^M ^M
ÿØÿà^#^PJFIF^#^A^A^#^#^A^#^A^#^#ÿá^#XExif^#^#MM^#*^#^#^#^H^#^B^A^R^#^C^#^#^#^A^#^A^#^#<87>i^#^D^#^#^#^A^#^#^#
hi i am uploading audio/image file as below please check it and its working fine using the ASIHTTPRequest.
NSURL *audiourl = [NSURL URLWithString:#"Your Url"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
NSData *postData = [NSData dataWithContentsOfURL:SoundPath];
//SoundPath is your audio url path of NSDocumentDirectory.
[request addData:postData withFileName:#"mynewfile.png" andContentType:#"image/png" forKey:#"FileData"];
[request setDelegate:self];
[request startasynchronous];
Thanks

How to download audio files

I want to download audio files Asynchronously from internet through ASIHTTP request. I have written a piece of code, but it's not working properly.
+(ASIHTTPRequest *)getDownloadedLectureAndSeries:(id)target :(NSString *)downloadString FinishSelector:(SEL) finishselector FailSelector:(SEL) failselector
{
NSString *api=downloadStrin;
NSURL *url = [NSURL URLWithString:api];
[api release];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request startAsynchronous];
[request setDelegate:target];
[request setDidFinishSelector:finishselector];
[request setDidFailSelector:failselector];
return request;
}
Help me, if you can. Thanks in advance.
Doesn't look like you are acctually saving the file anywhere in your code. (Unless you are trying to do it in your finishselector.
Add this line to have the request automatically save the file
[request setDownloadDestinationPath:path];
Where path is a NSString to where you want the file to be stored (such as your Documents directory

ASIHTTPFormDataRequest- "Unable to create request (bad url?)"

I have the following requirement to call a service using an ASIHTTPRequest with a POST method call. When I execute the request I get the following error:
errorError Domain=ASIHTTPRequestErrorDomain Code=5 "Unable to create request (bad url?)" UserInfo=0x790b490 {NSLocalizedDescription=Unable to create request (bad url?)}
And heres my code:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:SampleURL]];
[request addPostValue:#"emailid" forKey:username.text];
[request addPostValue:#"password" forKey:password.text];
[request addPostValue:#"first_name" forKey:first_name];
[request addPostValue:#"last_name" forKey:last_name];
[request addPostValue:#"option" forKey:#"userSignup"];
request.delegate = self;
[request startSynchronous];
Can anyone help me on this?
NSString *safestring=[strUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url= [NSURL URLWithString:safestring];
[request addPostValue:#"emailid" forKey:username.text];
Shouldn't that be in the opposite order: [request addPostValue:username.text forKey:#"emailid"];
Same for the rest...
It looks like that 'sampleURL' has some issue. What is the data type of sampleUL? it should be NSURL.
Also what is in sampleURL when this piece of code is executed.

creating JSON request URL in Objective C via DropBox SDK for iPhone

I'm rather new to trying to figure out how JSON and the DropBox API function with the iphone SDK that DB has release for the iPhone. I understand how both technologies work in general and that you have to build a dictionary into a request and get another dictionary back, but I can't find a solid example of how to do anything specifically enough online in Objective C.
I just need one example of how to retrieve for instance the user's profile information by creating a JSON request to fetch info from the drop-box server.
I've been able to log the user in and linking the device using the consumer key and consumer secret but what's next I'm a little at a loss.
MANY thanks for any guidance or examples.
To send your data
Example for POST methods:
url = #"https://api.dropbox.com/<version>/account/"
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:value forKey:#"callback"];
Example for GET methods (URL queries):
NSString *urlString = [[[NSString alloc] initWithString:#"https://api.dropbox.com/<version>/account/info
"] autorelease];
urlString = [urlString stringByAppendingFormat:#"?callback=whatever&status_in_response=something"];
NSURL *url = [[NSURL alloc] initWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDidFinishForThreadID:)];
[request startAsynchronous];
To retrieve JSON values and convert them into Dictionary
SBJsonParser *json = [[SBJsonParser alloc] init];
NSDictionary *dict = (NSDictionary*)[json objectWithString:responseString];
You will need JSON Framework: http://code.google.com/p/json-framework/
And also ASIHTTPRequest: http://allseeing-i.com/ASIHTTPRequest/
Haven't tested it with dropbox, but should be this way.

Parsing Data returned from Twitpic API

I just wanted to ask you if anyone can help me parsing the returned data from the Twitpic API?
I'm creating a HTTPFormRequest using the ASIHTTPRequest Wrapper for Cocoa. This all happens in an iPhone application:
NSURL *url = [NSURL URLWithString:#"http://twitpic.com/api/upload"];
NSString *username = t_user;
NSString *password = t_pass;
NSData *twitpicImage = UIImagePNGRepresentation(imageView.image);
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:twitpicImage forKey:#"media"];
[request setPostValue:username forKey:#"username"];
[request setPostValue:password forKey:#"password"];
[request setData:twitpicImage forKey:#"media"];
// Initiate the WebService request
[request start];
if ([request error]) {
NSLog(#"%#", [request error]);
} else if ([request responseString]) {
NSLog(#"%#", [request responseString]);
}}
Now comes the hard part, I don't know how to parse the data that is in [request responseString]. I know I need to use NSXMLParser, but I dunno how to use it. All I need is to get the url of the image.
Thx in advance.
Feel free to have a look at my little XML parse classes here http://www.memention.com/blog/2009/10/31/The-XML-Runner.html
I have started to use them for parsing the response from image upload to yfrog.com
Basically I do like this...
In NameValueParser.m I changed the entry tag to rsp like this
entryName = [[NSString stringWithString:#"rsp"] retain];
then where the response has been received I parse it like this
NameValueParser *parser = [NameValueParser parser];
[parser addFieldName:#"statusid"];
[parser addFieldName:#"userid"];
[parser addFieldName:#"mediaid"];
[parser addFieldName:#"mediaurl"];
[parser addFieldName:#"err"];
[parser parseData:responseData]; // the response received by ASIHTTPRequest
NSArray *rspArray = [parser list];
NSLog(#"%#", rspArray); // Have a look at it here
Try it as written at the bottom of this tutorial click here using NSScanner. They are showing exactly what you need, retrieving only the mediaurl = URL of uploaded image.
NSScanner *scanner = [NSScanner scannerWithString:responseString]; ...
GSTwitPicEngine does XML and JSON parsing both: http://github.com/Gurpartap/GSTwitPicEngine
Though, why not use JSON format for the Twitpic API responses? It's easy to parse and deal with using yajl, TouchJSON, json-framework or other Cocoa JSON libraries