gpt4 book ai didi

http - Flickr API iOS 应用程序 "POST size too large!"

转载 作者:可可西里 更新时间:2023-11-01 17:03:59 26 4
gpt4 key购买 nike

我正在尝试使用 API 将照片从 iOS 应用程序发送到 Flickr。我已成功获得授权并准备好使用 token 。我正在通过 HTTP POST 发送照片数据,并遵循此处的格式化指南。

http://www.flickr.com/services/api/upload.example.html

但是我从 API 得到的响应是...

<err code="93" msg="POST size too large! Try something smaller, mmkay?" />

我读到它可能与边界有关。我已经仔细检查了@samrowlands 对这篇文章的评论。 http://www.flickr.com/groups/api/discuss/72157605534260892/

任何帮助都会很棒。谢谢!

UIImage *image = [UIImage imageNamed:@"photo.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.9);

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://api.flickr.com/services/upload/"]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------7d44e178b0434"];

[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField: @"Content-Type:"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"%@\r\n", apiKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"%@\r\n", apiToken] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

NSString *uploadSig = [self md5HexDigest:[NSString stringWithFormat:@"%@api_key%@auth_token%@", apiSecret, apiKey, apiToken]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", uploadSig] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

最佳答案

我通过更改 Content-Type(无冒号)并在 sig 之后和照片之前添加边界来实现它。感谢您提供代码 - 希望对您有所帮助。

NSString *uploadSig = [[NSString stringWithFormat:@"%@api_key%@auth_token%@", secret, api_key, auth_token] MD5];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSString *url = [NSString stringWithFormat:@"http://api.flickr.com/services/upload/"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];


NSString *boundary = [NSString stringWithString:@"---------------------------7d44e178b0434"];

[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", api_key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", auth_token] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", uploadSig] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

关于http - Flickr API iOS 应用程序 "POST size too large!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4683559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com