Simple http request in gnustep's objective c not working - objective-c

#import <Foundation/Foundation.h>
main() {
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Data were download below:\n%#", [[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding]);
}
run the executable ->
Data were download below:
[null]
Why?

Have you tried allocating the request and error?
NSURLResponse *response = [[NSURLResponce alloc] init];
NSError *error = [[NSError alloc] init];

Related

How to check JSON post return value

I am new to JSON and I am trying to send a post I am wondering if how can I check if I did it properly or check the return value of it. Here's what I've done
NSURL *url = [NSURL URLWithString:#"http://json.myurl.com/.....];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
#"email", #"Email",
#"password", #"FirstName",
nil];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
[request setHTTPBody:postdata];
For a beginner, I would recommend using third party framework, widely used by iOS developers across the world, called AFNetworking.
By using AFNetworking, HTTP requests are simple as that:
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
// TODO: Parse success here!
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// TODO: Parse failure here!
}];
In given example, object responseObject is a representation of API response JSON object.
Installation and further usage instructions of AFNetworking can be found in their website.
To check JSON POST return value:
NSString *strUrl=[NSString stringWithFormat:#"http://json.myurl.com/....."];
NSString *webStringURL = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:webStringURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
#"email", #"Email",
#"password", #"FirstName",
nil];
NSError* error;
NSData* postData = [NSJSONSerialization dataWithJSONObject:tmp options:NSJSONWritingPrettyPrinted error: &error];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[postData length]];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *jsonData, NSError *error)
{
if (!error)
{
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
dispatch_async(dispatch_get_main_queue(),^
{
NSLog(#"jsonResponse--->%#",jsonResponse);
});
}
else
{
dispatch_async(dispatch_get_main_queue(),^
{
NSLog(#"error--->%#",error.description);
});
}
}];
And to check you JSON format is correct or not go through this link

Objective-C Post request send no data

I'm trying to send post request to an url
but the server always get empty data
NSString *post = [NSString stringWithFormat:#"cmd=login&account=%#&password=%#" ,account ,password];
NSData *data = [post dataUsingEncoding:NSASCIIStringEncoding];
NSMutableData *postData = [data mutableCopy];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://myurl.com"]]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse* urlResponse = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSLog(#"%#", returnString);
NSLog(#"error: %#", error.localizedFailureReason);
here is the response
2014-08-18 17:21:52.332 SignPos[2755:303] {"resp":99,"desc":"invalid cmd"}
2014-08-18 17:21:52.333 SignPos[2755:303] error: (null)
is there anything wrong?
thanks!

Vimeo.com upload failure

To have an ability to upload i have to get upload ticket. But after checking it looks like invalid.
After I create new ticket I check it and get the error 702. Please help. i've checked the parameters and they looks fine.
//vimeo.videos.upload.getTicket
NSURL *ticketRequestURL = [NSURL URLWithString:#"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.upload.getTicket"];
NSMutableURLRequest *ticketRequest = [NSMutableURLRequest requestWithURL:ticketRequestURL];
[ticketRequest setHTTPMethod:#"POST"];
[auth authorizeRequest:ticketRequest];
[NSURLConnection sendAsynchronousRequest:ticketRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *ticketRequestResponse, NSData *ticketRequestData, NSError *ticketRequestError) {
NSString *ticketRequestResponseString = [[NSString alloc] initWithData:ticketRequestData encoding:NSStringEncodingConversionAllowLossy];
NSDictionary *d = [[parser objectWithString:ticketRequestResponseString] objectForKey:#"ticket"];
NSString *identifier = [d objectForKey:#"id"];
//vimeo.videos.upload.checkTicket
NSURL *ticketCheckRequestURL = [NSURL URLWithString:#"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.upload.checkTicket"];
NSMutableURLRequest *ticketCheckRequest = [NSMutableURLRequest requestWithURL:ticketCheckRequestURL];
[ticketCheckRequest setHTTPMethod:#"POST"];
[ticketCheckRequest setValue:identifier forHTTPHeaderField:#"ticket_id"];
[auth authorizeRequest:ticketCheckRequest];
[NSURLConnection sendAsynchronousRequest:ticketCheckRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *ticketRequestResponse, NSData *ticketRequestData, NSError *ticketRequestError) {
NSString *ticketCheckRequestResponseString = [[NSString alloc] initWithData:ticketRequestData encoding:NSStringEncodingConversionAllowLossy];
NSLog(#"%#", ticketCheckRequestResponseString);
}];
}];
Hm... Specifying ticket_id in the URL solved my problem.
NSURL *ticketCheckRequestURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.upload.checkTicket&ticket_id=%#", identifier]];

NSURLConnection (Syncronous Request) returns (null)

I want to get HTML from website. I wrote below code but this returns (null).
NSString *strURL = #"http://www.googole.com";
- (NSString *)getHtml
{
NSURL *url = [NSURL URLWithString: strURL];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest : req
returningResponse : &response
error : &error];
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return strData;
}
I think you have mistyped the url.
It should be NSString *strURL = #"http://www.google.com";

NSURLConnection sendSynchronousRequest - missing data

I'm trying to read a text file using a synchronous request. It doesn't work but I get no errors or warnings either.
Can anyone enlighten me on what I'm doing wrong, please?
NSString *url = #"http://pappons.com/test.txt" ;
NSLog(#"getHTTPData: %#" , url ) ;
NSURLResponse* response = nil;
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil] ;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog( #"data: %#" , myString ) ;
output:
2012-06-15 11:33:42.209 FrederikTest[1365:707] getHTTPData: http://pappons.com/test.txt
2012-06-15 11:33:42.306 FrederikTest[1365:707] data:
pass in NSError to check if error occurred
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];