Question

i have this URL and I am trying to shorten it through bit.ly API. Here is my code

NSString *url = @"http://www.example.com&username=abc&password=123&mode=offline";

NSString *requestStr = [NSString stringWithFormat:@"http://api.bit.ly/v3/shorten?login=%@&apiKey=%@&longUrl=%@&format=txt",login, api_key, url];

    requestStr = [requestStr stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestStr]];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *response = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

I am getting a url in response which corresponds to only http://www.example.com

Surprisingly, &username=abc&password=123&mode=offline parts of the url are trimmed. This happens only when I am doing it through xcode. On the website, it is working properly. PLease help.

Was it helpful?

Solution

NSString *url = @"https://www.googleapis.com/urlshortener/v1/url?key=UR_KEY";

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"POST"];

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"UR_LONG_URL",@"longUrl", nil];

    [request setHTTPBody:[[dict JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding]];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *response = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top