1

サーバーは「token」と「name+_time」の一部を認識しません

私のコード

NSArray *keysParams = [NSArray arrayWithObjects:@"time",nil];
NSArray *valuesParams = [NSArray arrayWithObjects:time, nil];
NSDictionary *dictParams = [NSDictionary dictionaryWithObjects:valuesParams       forKeys:keysParams];

NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictParams options:NSJSONWritingPrettyPrinted error:&writeError];


NSData *imageData = UIImageJPEGRepresentation(getImage1, 0.8);
NSString *boundary = @"&&3rewfwefwfewfhufrbewfuweriwefr";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];

//this appends the image
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"photo.jpg\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
//json
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@_time\" \r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:jsonData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//token
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"token\" \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

リクエストボディ

imagename(file)
/imagename_time/ - part name
{
time:100
}
/token/ - part name
"string"

どこかにバグがありますが、見つけられません。初めてマルチパート投稿を書きます。ありがとう!

4

1 に答える 1