Repository navigation
Expand file tree
/
Copy pathTNSHttpFormData.ios.ts
More file actions
38 lines (35 loc) · 1.74 KB
/
Copy pathTNSHttpFormData.ios.ts
File metadata and controls
38 lines (35 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { TNSHttpFormDataParam, TNSHttpFormDataResponse } from "./index";
import { Common } from "./TNSHttpFormData.common";
export class TNSHttpFormData extends Common {
constructor() {
super();
}
post(url: string, params: Array<TNSHttpFormDataParam>): Promise<TNSHttpFormDataResponse> {
return new Promise((resolve, reject) => {
let multipartFormData = OMGMultipartFormData.new();
for (let param of params) {
if (param.fileName && param.contentType) {
multipartFormData.addFileParameterNameFilenameContentType(
param.data, param.parameterName, param.fileName, param.contentType);
} else {
multipartFormData.addTextParameterName(param.data, param.parameterName);
}
}
let request: NSMutableURLRequest = OMGHTTPURLRQ.POSTError(url, multipartFormData);
NSURLConnection.sendAsynchronousRequestQueueCompletionHandler(
request, NSOperationQueue.currentQueue, (response: NSHTTPURLResponse, data: NSData, error: NSError) => {
if (error) {
reject(error);
return;
}
let responseData: TNSHttpFormDataResponse = {
headers: response.allHeaderFields,
statusCode: response.statusCode,
statusMessage: NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode),
body: JSON.parse(NSString.alloc().initWithDataEncoding(data, NSASCIIStringEncoding).description)
}
resolve(responseData);
});
});
}
}