简体   繁体   中英

Error 400 uploading Image data to device with NSURLSessionDataTask

I am attempting to upload a PNG file to a device, using NSURLDataTask and a "POST" request. However my attempts always conclude with the device returning a 400 HTTP error code in the response.

My Upload code looks like this:

-(void)uploadMedia:(NSData*)mediaData withName:(NSString*)name andID:(NSString*)mediaID

{ NSString *urlString;

NSString *appToken [self retrieveAppToken];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                      appToken, @"km-device-auth",
                      @"application/octet-stream", @"Content-Type",
                      nil];

sessionConfiguration.HTTPAdditionalHeaders = dict;

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

urlString = [NSString stringWithFormat:@"https://%@:7788/Media?name=%@&id=%@",_strIP,name,mediaID];

NSLog(@"Attemptive Upload URL:%@",urlString);

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];


request.HTTPMethod = @"POST";
request.HTTPBody = mediaData;

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"My respone:%@:%@",response,data);
}];


[postDataTask resume];

}

已解决我的标题还需要一个缺失的“Content-MD5”值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM