Creating XML requests and sending them to server using ASIHttpRequest - iphone

I am trying to generate XML request to be sent to server over Http Post. I am aware of ASIHttpRequest library and want to use it.
I have two questions regarding this:
1) How to generate XML request? (Which is best library to use? GData/libXML/KissXML? or else?)
2) How to send it to server using ASIHttpRequest?
Please help.
Thanks in advance.

Achieved it this way:
NSString* newStr = [[NSString alloc] initWithData:postBody
encoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlstr];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"r" forKey:newStr];
request.delegate = self;
[request startSynchronous];

Related

What I did is correct or not? When I send an audio file to server in NSData

I am recording audio in a .wav format and converting the .wav1 file to NSData and sending to server.
Recorded path is:
file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav
I am sending to the server using:
audioURL=#"file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav";
NSURL *url=[NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod: #"POST"];
[request setValue: #"multipart/form-data" forHTTPHeaderField: #"Content-Type"];
NSData *audiodata = [NSData dataWithContentsOfURL:audioURL];
NSMutableData *highScoreData = [NSMutableData data];
[highScoreData appendData:audiodata];
[request setHTTPBody:highScoreData];
nsurlConnection = [[NSURLConnection alloc] initWithRequest: request
delegate: self];
When I play this back it gives me the right recorded voice. However, when I play the same recorded voice from the server it says "quotation" instead of the actual recorded voice.
My full code of how I did audio recording and how i send the audio can be found here.
Please tell me if the way I did this(i.e, to sending to server) is correct or not?
Multipart requests don't work that way. You need boundaries to differ between different data. Please check: http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
For the solution, I use AFNetworking (https://github.com/AFNetworking/AFNetworking).
The AFHTTPClient can create multipart requests for you.
Check: http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:
Here is the code I use to upload. The header part depends on how your server handles this.
AsiFormdata is more proper here.
NSString* fileString;
fileString = [[self documentsPath]
stringByAppendingPathComponent:#"testcombine.m4a"];
NSData* songData=[NSData dataWithContentsOfFile:fileString];
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://%#/UsingWebServer2/UploadServlet",RemoteEndpoint]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:songData withFileName:#"upload_test.m4a" andContentType:#"audio/m4a" forKey:#"file"];
//[request appendPostData:[Jstring dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
If you want to send the data via multipart/form-data, use ASIFormDataRequest. Find documentation here for more reference.

error.USER_REQUIRED generated when attempting to submit Reddit link in objective-c

I am having a small Reddit API problem here. I am trying to follow the reddit api, as outlined here:
https://github.com/reddit/reddit/wiki/API
Logging in using a simple NSMutableURLRequest is not a problem:
NSString *user = [[NSString alloc]initWithString:[userFld text]];
NSString *passwd = [[NSString alloc]initWithString:[passwordFld text]];
NSString *urlString = [NSString stringWithFormat:#"http://www.reddit.com/api/login/username/?user=%#&passwd=%#",
[user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[passwd stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Which gives me a result from which I extract the user mod-hash:
4029916%2C2010-04-30T22%3A51%3A52%2C1243925043100000000000000000000000000000
Next I am trying to post, using:
NSString *redditUrlString = [NSString stringWithFormat:
#"http://www.reddit.com/api/submit/?uh=%#&kind=link&url=%#&sr=%#&title=%#&r=%#&api_type=json",
[appDelegate globalUhString],
#"www.google.com",
#"funny",
#"Google.com",
#"funny"];
NSURL *url = [NSURL URLWithString:redditUrlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
However, when I extract the JSON response form the data received from this connection, I always receive the error below no matter what I use as the mod-hash:
[".error.USER_REQUIRED"]
Can someone explain what I have done incorrectly/how I can fix it?
It looks like you've not passing in the reddit_session cookie along with the uh parameter. The api documentation seems to explain the reason for receiving the error. According to this answer, it looks like you may need to issue the code:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
in the applicationDidBecomeActive handler, as it may be rejecting the cookies due to the CookieAcceptPolicy being shared amongst all applications.

ASIHTTPRequest setPostValue iPhone

I'm accessing a PHP script on a server.
The request URL is like this:
example.com/cgi-bin/getEvent.cgi?EID=19573
When I put in the request via a browser, I get back my expected results.
However when I use the ASIHTTP Form request, I'm getting back a result
like the EID isn't being passed via HTTP.
NSString *eventID = #"19573";
NSString * const EVENT_URL = #"http://example.com/cgi-bin/getEvent.cgi";
-(void)callWebservice
{
NSURL *url = [NSURL URLWithString:EVENT_URL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:eventID forKey:#"EID"];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDelegate:self];
[request startAsynchronous];
}
Anyone know of a method to see the full URL being requested?
Or have any clue why this wouldn't be working?
Thanks in advance.
My guess is that your PHP script is expecting the parameter to be sent on the query string (i.e. as a GET request) rather than as a POST parameter.
If that's the case, you can fix it be sending your request as follows:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#?EID=%#",EVENT_URL,eventID]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

Objective-C and Preemptive Authentication

I am trying to get data from WebService for my Ipad App.
To do that, I am using a NSURLConnection with NSMutableURLRequest.
Web Services are published on Apache-Coyote 1.1 with Preemptive Basic Authentication, but I don't know how to send my credentials from Objective-C.
Anybody knows how can I set my user/password in Objective-C to log my clients with the Apache premptive authentication system?
Thank you.
EDIT:
In order to set the creds yourself, you'll need to be able to base64 encode the username and password and set the appropriate header. Matt Gallagher, from Cocoa With Love, has a great post on how to add a category to NSData to easily do this.
NSString* username = #"username";
NSString* password = #"password";
NSString* encodedUsername = [[username dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString* encodedPassword = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSURL* url = [NSURL URLWithString:#"http://yourUrl.com/"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
NSString* headerValue = [NSString stringWithFormat:#"Basic %#:%#", encodedUsername, encodedPassowrd];
[request addValue:#"Authorization" forHTTPHeaderField:headerValue];
[NSURLConnection connectionWithRequest:request delegate:self];
As with all use of credentials, please make sure you are doing this all over HTTPS because these credentials are essentially being passed in clear text.
Consider using the ASIHTTPRequest framework, which makes preemptive Basic authentication requests simple:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; /** preemptively present credentials **/
[request setUsername:#"username"];
[request setPassword:#"password"];
[request startSynchronous];
NSError *error = [request error];
if (!error)
NSString *response = [request responseString];
And, yes, you definitely want to do Basic authentication over SSL (i.e. via some https://... URL).

Post request from iPhone using ASIFormDataRequest not working

Newbie to Rails/iOS here. I have a rails blog application. I'm trying to upload a post from iOS using an HTTP POST method with ASIFormDataRequest. Here's the code that get's called:
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setFile:#"star.png" forKey:#"image"];
[request startSynchronous];
NSString *response = [request responseString];
NSLog(#"response:: %#", response);
When I run this code, nothing happens. My server does not get contacted. I get response:: (null). Any idea why?
EDIT I found out that star.png needed to have its full file address. Now the POST works fine, but neither the name or image get saved into the db. A new post with empty name and image gets created. Any idea why?
why you are using localhost here?
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
use your web server ip instead of localhost
NSURL *url=[[NSURL alloc] initWithString:#"http://yourservername:3000/posts"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setPostValue:imageData forKey:#"image"];
[request startSynchronous];
For the "imagePath" is the path to your image. The full path.