邮递员代码在Xcode中不起作用

时间:2018-08-06 08:21:27

标签: objective-c post postman

即时通讯发出了一个发布请求,它在邮递员上返回了成功,而我从下面的邮递员那里得到了我的代码:

NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
                           @"x-session": @"8a1c910b9e7ba661f4425cc971fee1e9ca45b208",
                           @"cache-control": @"no-cache",
                           @"postman-token": @"d237e02e-ec8b-17fd-adf0-940a7fa03659" };
NSArray *parameters = @[ @{ @"name": @"contacts[0][company_name]", @"value": @"Ali" },
                         @{ @"name": @"contacts[0][type_id]", @"value": @"5" },
                         @{ @"name": @"contacts[0][contact_fname]", @"value": @"jaber" },
                         @{ @"name": @"contacts[0][contact_lname]", @"value": @"jaber2" },
                         @{ @"name": @"contacts[0][contact_phone]", @"value": @"70034491" },
                         @{ @"name": @"contacts[0][remark]", @"value": @"sdsdd" },
                         @{ @"name": @"contacts[0][images][0]", @"value": @"https://otrackapi.omegasoftware.ca/ActStaff/public/uploads/user-image-94.jpg" },
                         @{ @"name": @"contacts[0][lat]", @"value": @"33.311" },
                         @{ @"name": @"contacts[0][lng]", @"value": @"33.234" },
                         @{ @"name": @"contacts[0][contact_email]", @"value": @"me@me.com" } ];
NSString *boundary = @"----WebKitFormBoundary7MA4YWxkTrZu0gW";

NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
    [body appendFormat:@"--%@\r\n", boundary];
    if (param[@"fileName"]) {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
        [body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
        [body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];
        if (error) {
            NSLog(@"%@", error);
        }
    } else {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
        [body appendFormat:@"%@", param[@"value"]];
    }
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://xxxxxxxxxxxxx"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

但是当我将此代码放入xcode发出请求时,它没有返回成功,我得到一个错误,这是找不到正文中的项目,例如company_name等。 任何人都可以提供帮助,或者有任何想法吗?

0 个答案:

没有答案