nemesis
nemesis

Reputation: 1351

iOS - REST Client works fine while NSURLRequest returns NULL

I'm trying to download a file from a cloud via a GET HTTP request, and in my REST Client (Postman for Chrome) I get the results as expected, but when I pass the request in iOS, I get a NULL response. What could be the problem?

In my REST Client, I pass the URL as http://myserver.com/api/download.json?filepath=/&fileid=document:1pLNAbof_dbXGn43GtMNafABMAr_peTToh6wEkXlab7U&filename=My Info.doc with a header field for a authorization key, and it works perfectly fine. While in iOS, if I do such a thing:

[request setURL:[NSURL URLWithString:URLString]]; // I tried even to hardcode the 
                                                 // URLString to be one of the 
                                                // working URLs from the Postman 
                                               // Client, doesn't work as well.
[request setHTTPMethod:@"GET"];
NSData* response = [NSURLConnection sendSynchronousRequest:request 
                                          returningResponse:&urlResponseList
                                          error:&requestErrorList];

response is null, and I get a status code 500. Anyone know how to make this work? Thanks.

Upvotes: 0

Views: 1195

Answers (2)

andrew lattis
andrew lattis

Reputation: 1549

it looks like your URL has a space in it, you'll need to escape that before sending the string to NSURL.

it would also be helpful to know what the full error from your server is if this doesn't work. what does requestErrorList contain?

but if the only problem is the non-escaped characters something like this should work.

NSString *encodedString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setURL:[NSURL URLWithString:encodedString];

Upvotes: 1

NSMutableString
NSMutableString

Reputation: 10743

An HTTP status code 500 occurs when the webservice is not able to handle your request. An internal error occured.

Try to add some more details to your request.

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

Upvotes: 1

Related Questions