使用 PUT 方法上传图像,如 Walgreens API for iOS 列出的步骤

问题描述 投票:0回答:1

我正在使用“Walgreens 照片打印”API 从我的 iOS 应用程序下订单。

在这个应用程序中,我最后需要上传图像并在下订单之前使用该图像的 URL。

为此,沃尔格林开发者门户给出了几个需要遵循的步骤。

请查看以下链接中的主题“上传一组图像是一个 4 步过程:”

https://developer.walgreens.com/api/photoprints/rest

我已按照这些步骤操作,这是我的代码。

- (void) callMethodToUploadImages:(NSString *)authData andurlString:(NSString *)urlStr imageData:(NSData *)imageData seesionID:(NSString *)sessionID andDate:(NSString *)currentDate{

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPShouldSetCookies = NO;
config.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];

manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"image/jpg" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"image/jpg" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:urlStr forHTTPHeaderField:@"Host"];
[manager.requestSerializer setValue:currentDate forHTTPHeaderField:@"Date"];
[manager.requestSerializer setValue:authData forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"%ld",[authData dataUsingEncoding:NSUTF8StringEncoding].length] forHTTPHeaderField:@"Content-Length"];
[manager.requestSerializer setValue:@"100-continue" forHTTPHeaderField:@"Expect"];
[manager.requestSerializer setValue:sessionID forHTTPHeaderField:@"x-amz-security-token"];
[manager PUT:[urlStr stringByAppendingString:@"/myImage_01_30_2015_12154510.jpg"] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
}];
}
- (NSData *)hmacsha1:(NSString *)data secret:(NSString *)key {
const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];

unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
                                      length:sizeof(cHMAC)];

NSData * hash = [HMAC base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];

return hash;}

当我运行此方法时,它以以下错误响应我:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x1465cced0> { URL: http://pod-qa.walgreens.com/myImage_01_30_2015_12154510.jpg } { status code: 400, headers {
Connection = close;
Date = "Fri, 08 Apr 2016 07:16:20 GMT";
Server = AmazonS3;
"Transfer-Encoding" = Identity;} }, NSErrorFailingURLKey=http://pod-qa.walgreens.com/myImage_01_30_2015_12154510.jpg, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: bad request (400)}

当我使用此错误代码进行搜索和挖掘时,我发现服务器根本不理解我们正在发出的请求。

请协助我成功完成这项任务。

谢谢。

ios objective-c alamofire
1个回答
0
投票

请尝试将标头限制为以下简单的 PUT 结构:

class PUT:NSObject{

let request:NSMutableURLRequest
let session:NSURLSession

init (url: String, auth: String, imageContents:NSData, imageLength:String, sessionID:String, done: (NSData?, NSURLResponse?, NSError?) -> Void)
{

    self.request = NSMutableURLRequest(URL: NSURL(string:url)!)
    self.request.HTTPMethod = "PUT"
    let api:APIHandler = APIHandler()

    self.request.addValue(api.getDateString(), forHTTPHeaderField: "Date")
    self.request.addValue(auth, forHTTPHeaderField: "Authorization")
    self.request.addValue("image/jpg", forHTTPHeaderField: "Content-Type")
    self.request.addValue(imageLength, forHTTPHeaderField: "Content-Length")
    self.request.addValue(sessionID, forHTTPHeaderField: "x-amz-security-token")

    session = NSURLSession.sharedSession()
    request.HTTPBody = imageContents

    let task = session.dataTaskWithRequest(request, completionHandler: done)
    task.resume()
}
}
© www.soinside.com 2019 - 2024. All rights reserved.