https google urlshortener request missing body
- by Peter
Hi,
Just trying to get the new API for the goo.gl URL shortening service working on my iPhone, following the instructions on http://code.google.com/apis/urlshortener/v1/getting_started.html
I'm set up and the API is enabled etc., but when I send a request in the recommended format:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}
I get an error returned. The error is exactly the one listed on that page in the errors section for if you haven't passed in a longURL param. This makes me think that I'm not setting up the body of the POST request properly. Here's the code if you have any pointers...
NSString *longURLString=@"http://www.stackoverflow.com";
NSString *googlRequestString=@"https://www.googleapis.com/urlshortener/v1/url";
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:googlRequestString]];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type:"];
NSString *bodyString=[NSString stringWithFormat:@"{\"longUrl\": \"%@\"}",longURLString];
[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *theResponse;
NSError *error=nil;
NSData *receivedData=[NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&error];
NSString *receivedString=[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Received data: %@",receivedString);
[receivedString release];
The NSLog returns:
Received data: {
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required",
"locationType": "parameter",
"location": "resource.longUrl"
}
],
"code": 400,
"message": "Required"
}
}
which is exactly what Google says you get if you have not passed a longUrl parameter....
My guess is I'm missing something very obvious here :-)
P