This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathobjc.html
More file actions
36 lines (29 loc) · 1.72 KB
/
objc.html
File metadata and controls
36 lines (29 loc) · 1.72 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
<section name="objc" class="objc">
<p class="ioDesc">Request</p>
<pre class="incoming"><code class="language-objc">NSURL *URL = [NSURL URLWithString:@"<%= @apiUrl %><%= @url %>"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"<%= @method.toUpperCase() %>"];
<% if @headers and @helpers.isNotEmpty @headers: %>
<% for header, value of @headers: %>
[request setValue:@<%= @helpers.escape value %> forHTTPHeaderField:@<%= @helpers.escape header %>];
<% end %>
<% end %>
<% if @body?.length > 0: %>
[request setHTTPBody:[@<%= @helpers.escape @body %> dataUsingEncoding:NSUTF8StringEncoding]];
<% end %>
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// Handle error...
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
}
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response Body:\n%@\n", body);
}];
[task resume];</code></pre></section>