Objective c NSMutableURLRequest格式的cURL upload photo命令
我知道这可能是手头的问题所特有的,但是,这是我试图避免使用可怕的UploadCare,因为它包含大量(而且已经过时)的依赖项,只是上传一个文件(然后在以后下载) 我正在努力转换cURL命令中的“-F”选项。我知道他们指定了HTTP多部分POST数据,但很难将其转换为附带图片文件的NSMutableData。我一直收到403状态码 cURL命令是:Objective c NSMutableURLRequest格式的cURL upload photo命令,objective-c,curl,upload,nsmutabledata,uploadcare,Objective C,Curl,Upload,Nsmutabledata,Uploadcare,我知道这可能是手头的问题所特有的,但是,这是我试图避免使用可怕的UploadCare,因为它包含大量(而且已经过时)的依赖项,只是上传一个文件(然后在以后下载) 我正在努力转换cURL命令中的“-F”选项。我知道他们指定了HTTP多部分POST数据,但很难将其转换为附带图片文件的NSMutableData。我一直收到403状态码 cURL命令是: curl -F "UPLOADCARE_PUB_KEY=e84a031b3da1g560d56d" \ -F "UPLOADCARE_STORE=1
curl -F "UPLOADCARE_PUB_KEY=e84a031b3da1g560d56d" \
-F "UPLOADCARE_STORE=1" \
-F "file=@aaronmillman.jpg" https://upload.uploadcare.com/base/
我目前的尝试是:
NSMutableData *body = [NSMutableData data];
NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
if (imageData) {
[body appendData:[@"UPLOADCARE_PUB_KEY=e84a031b3da1g560d56d" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"UPLOADCARE_STORE=1" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"file="] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
}
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:baseUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = imageData;
NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@",response);
}];
[uploadTask resume];
NSMutableData有什么问题
第二个相关的问题是:是否值得使用objective-C库卷曲包装器
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.uploadcare.com/base/"]];
UIImage *image = [UIImage imageNamed:@"image.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[request setHTTPMethod:@"POST"];
NSString *boundary = @"unique-consistent-string";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"UPLOADCARE_PUB_KEY"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", @"demopublickey"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"UPLOADCARE_STORE"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", @"1"] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=file; filename=image.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"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data.length > 0) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
}];
您是否正在实现(void)URLSession:(NSURLSession*)session didReceiveChallenge:方法?403可能与此相关。如中所示,我不应该调用该委托方法?我运行的唯一代码就是我在这里发布的代码(我想没有必要在委托调用中引用self):我不太确定我应该用这个委托方法做什么,但是,不管怎样,当我现在刚刚添加它时,它似乎什么都没有做。嗨,Rusik,当autostore关闭且UPLOADCARE_STORE==1时,当我使用此选项时,它会给我一个403:当autostore关闭时,我如何在服务器上存储文件?autostore-是上载期间存储文件的选项。您需要在项目设置中允许自动存储才能使其正常工作。常规存储请求REST API,可在上传后24小时内完成: