使用AFNetworking调用Google Cloud Vision API遇400错误求助
Hey there, that 400 error you're hitting is almost definitely from an incorrect JSON request structure—your features block is sitting in a separate object inside the requests array instead of being paired with the image object. Let's fix this step by step:
The Core Issue in Your Code
Google Cloud Vision expects every entry in the requests array to be a single, complete request object that includes both the image data and the features you want to run on it. Right now, you've split these two parts into two separate objects in the array, which breaks the API's expected format.
Here's your original code snippet for reference:
NSString *base64 = [self encodeToBase64String:[UIImage imageNamed:@"IMG_5081.JPG"]]; NSDictionary *d3 = @{@"requests": @[ @{@"image":@{@"content":base64}}, @{@"features":@[@{@"type":@"LABEL_DETECTION",@"maxResults":@(1)}]} ]}; // AFHTTPSessionManager code follows...
The Corrected Request Structure
You need to nest both image and features under the same dictionary in the requests array. Here's the fixed version, including the full AFNetworking call for context:
NSString *base64 = [self encodeToBase64String:[UIImage imageNamed:@"IMG_5081.JPG"]]; // Create a single request object with both image and features NSDictionary *annotateRequest = @{ @"image": @{@"content": base64}, @"features": @[ @{@"type": @"LABEL_DETECTION", @"maxResults": @(1)} ] }; // Wrap the request in the required top-level structure NSDictionary *requestBody = @{@"requests": @[annotateRequest]}; // Set up AFNetworking and send the request AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; // Don't forget to add your Google Cloud Vision API key here! [manager.requestSerializer setValue:@"YOUR_GOOGLE_API_KEY" forHTTPHeaderField:@"X-Goog-Api-Key"]; [manager POST:@"https://vision.googleapis.com/v1/images:annotate" parameters:requestBody progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"Success! Annotation results: %@", responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"Error: %@", error.localizedDescription); // For better debugging, log the full error response from Google if (task.responseData) { NSString *errorDetails = [[NSString alloc] initWithData:task.responseData encoding:NSUTF8StringEncoding]; NSLog(@"Full API error response: %@", errorDetails); } }];
Quick Tips to Avoid Future Headaches
- API Key: Double-check that you're using a valid, enabled Google Cloud Vision API key (it needs to be linked to a project with the Vision API turned on).
- Base64 Validation: Make sure your
encodeToBase64String:method produces a clean, URL-safe Base64 string with no extra whitespace or invalid characters—Google's API is strict about this. - Debugging: When you get a 400 error, always log the full response body from Google. It will usually tell you exactly what's wrong (like "Missing features in request" or "Invalid image content").
内容的提问来源于stack exchange,提问作者Teja Nandamuri

