Last active
November 2, 2017 07:29
-
-
Save zealinux/3d8e43f5579153ef9b8bc1ca449c3367 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", | |
| @"cache-control": @"no-cache", | |
| @"postman-token": @"184d155d-451d-bd9c-06f2-ae6274c61509" }; | |
| NSArray *parameters = @[ @{ @"name": @"file", @"fileName": @"/Users/wqn/Downloads/img-test/ym-1.jpeg" } ]; | |
| 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:@"http://52.80.80.183:8200/api/v1/test"] | |
| 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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment