I'm trying to convert most of a project's NSURLRequests that use NSURLConnection to ASIHTTPRequest calls. I came across an issue about setting the HTTPBody in an ASIHTTPRequest. Here's what I had for the NSURLRequest call:
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[self.paramString dataUsingEncoding:NSUTF8StringEncoding]];
[self _sendRequest:req];
And to convert to ASI, this is what I have so far:
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:self.urlString]];
[request setRequestMethod:#"POST"];
[request appendPostData:[self.paramString dataUsingEncoding:NSUTF8StringEncoding]];
[request setCompletionBlock:^{
NSLog(#"ASIHTTP request finished: %#", [request responseString]);
// Do stuff here
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"ASIHTTP error: %#", [error description]);
}];
[request startAsynchronous];
Although when I run this with ASI, I get this error:
ASIHTTP error: Error Domain=ASIHTTPRequestErrorDomain Code=6 "Unable to start HTTP connection" UserInfo=0x10ddf6b0 {NSLocalizedDescription=Unable to start HTTP connection}
EDIT This error was fixed, but data is still not being transmitted correctly due to the body not being set.
I'm thinking this has to do with me not setting the body correctly. I tried using ASI's setPostBody, but that only produced the same result. This works fine with NSURLRequest, but not ASI. I'm pretty sure it's really simple and I just haven't explored ASI's full library quite yet, but I was just wondering if anyone had any suggestions. I have read the documentation, but I couldn't find what I was looking for.
Thanks!
Could you check that
self.paramString
is correct? How does it look like?
I ended up using ASIFormDataRequest and setting values and keys. That seemed to work!
Related
I came into an issue trying to send files with NSURLSessionUploadTask to a custom REST service.
The problem is that the file seems to get transferred up to an arbitrary size, and then the task stops without any error (URLSession:task:didCompleteWithError: gets called with error set to nil).
The file that needs to be transferred is almost 10MB in size, and I found that smaller files sometimes get transferred correctly.
The code I'm using for creating the task is the following:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"100-continue" forHTTPHeaderField:#"Expect"];
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"%d", length] forHTTPHeaderField:#"Content-Length"];
NSURL *fileUrl = [NSURL fileURLWithPath:instanceFilePath];
NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileUrl];
I need to specify some headers for the server to correctly interpret the request, and the length variable is the effective size of the file being sent.
Any idea about what's going on here?
Thanks.
Its my code , can you help where am I missing to handle Memory? I'm using X-Code 4.6. And also I have checked instrument , to get other memory leaks. Its almost showing all "ASIHTTPRequest". Im not handling manually like [request release]; Is that necessary to fix memory leak? Thanks in advance
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
}];
[request setFailedBlock:^{
}];
[request setTimeOutSeconds:60];
[request startAsynchronous];
It leaks because of the retain cycle created between the block and the request object.
Try the below:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
__weak ASIHTTPRequest *weakRequest = request;
[request setCompletionBlock:^{
}];
[request setFailedBlock:^{
}];
[request setTimeOutSeconds:60];
[request startAsynchronous];
Cheers..
EDIT:
ASIHTTPRequest is not supported anymore, try to move to AFNetworking, it is the best.
Then instead do this:
__weak __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
__strong ASIHTTPRequest *requestInBlock = request;
//your code, and replace all refrences of request inside the block with requestInBlock.
}];
[request setFailedBlock:^{
//same here
}];
[request setTimeOutSeconds:60];
[request startAsynchronous];
try this out.
I'm using ASIHttpRequest to send data back and forth to the server in my IOS game.
I'm wondering if someone can see the url being send to the server, someway?
... If that's the case, cheating is possible.
thanks in advance
The request looks like this:
NSURL *url = [NSURL URLWithString: #"http://xx.xxx.xx.x/development/"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];
__weak ASIHTTPRequest *request_b = request;
[request setDelegate: self];
[request setQueue: [[DatabaseHelper sharedInstance] networkQue]];
[request setShouldContinueWhenAppEntersBackground: NO];
[request addRequestHeader:#"Content-Type" value:#"text/html; charset=utf-8;"];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setTimeOutSeconds: 20.0f];
[request setCachePolicy: ASIDoNotWriteToCacheCachePolicy | ASIDoNotReadFromCacheCachePolicy];
[request setPostValue:#"aSecretString" forKey:#"secret"];
[request setPostValue: #"updateUser" forKey:#"apiToRun"];
[request setPostValue: #"4" forKey:#"userId"];
url:
http://xx.xxx.xx.x/development/secret=aSecretString&apiToRun=updateUser&userId=4
With a GET request, data is sent as part of the URL and is easy to intercept with HTTP monitoring. If you use POST, this information will not be visible in the URL.
My question is:
How can I create the same request as below using ASIFormDataRequestand second as I think I need to parse JSON from response.
http://exampledomain.com/mobile_api/register/?client=iphone&info=A=iPhone/OS=5.1/C=UA&time=1342780143&udid=8b6f0cc104d137ae2e1730235f5664094b831122&version=1.0&secure=5444d72741bad93b916577d9297fa
Now I try to use code like this
NSString *strURL = #"http://test2.mafia.ua/mobile_api/register/";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
[request setDelegate:self];
[request setPostValue:#"iphone" forKey:#"client"];
[request setPostValue:#"A=iPhone/OS=5.1/C=UA" forKey:#"info"];
[request setPostValue:#"1342780143" forKey:#"time"];
[request setPostValue:#"8b6f0cc104d137ae2e1730235f5664094b831122" forKey:#"udid"];
[request setPostValue:#"1.0" forKey:#"version"];
[request setPostValue:#"5444d72741bad93b916577d9297fa" forKey:#"secure"];
[request startAsynchronous];
is this correct?
I implement delegate method also
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(#"Response %d ==> %#", request.responseStatusCode, [request responseString]);
}
- (void)requestFailed:(ASIHTTPRequest *)request {
}
In response I get this (I think this is JSON) if I use some browser.
{"status": "ok", "secret": "b82b7771f600772c2c5af903b117b5e", "client": "200004", "expires": "1345372850"}
You're passing your data in POST fields (That's what setPostValue: does.)
You should be including them in your URL instead; try searching around for an easy way of building query parameters in a URL or just use something like stringWithFormat: and appropriate escaping.
Hi I am using ASIHTTPRequest POST for adding time entry on basecamp for logged in user.
But
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
method of ASIHTTPRequest is getting called and in
- (void)requestFailed:(ASIHTTPRequest *)request
I am getting error as access denied.
I also need to send aunthentication token to basecamp URl please help me how to do this.
NSString *postData=[NSString stringWithFormat: #"<time-entry>\
<person-id> 898989 </person-id>\
<date>2011-04-07</date>\
<hours>2:00</hours>\
<description>test</description>\
</time-entry>"];
NSURL *url = [NSURL URLWithString:#"https://test.basecamphq.com/projects/4644/time_entries.xml"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request appendPostData:[postData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request addRequestHeader:#"Accept" value:#"application/xml"];
[request addRequestHeader:#"Content-Type" value:#"application/xml"];
[request authenticationNeeded];
[request setValidatesSecureCertificate:NO];
[request startAsynchronous];
This is my code. Please help.
Assuming this is the right API doc:
http://developer.37signals.com/basecamp/
Then basecamp using HTTP basic authentication, meaning you need to do this:
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
[request setUsername:yourApiToken];
[request setPassword:#"X"];
Putting your API token where it says 'yourApiToken'.