7

I have try to make a HTTP Get in Objective-C.

It use the NSMutableURLRequest *request; in Objective-C, and use [request setHTTPMethod:@"GET"]; to select GET or POST.

And set the Header via following code:

[request setValue:@"Application/json" forHTTPHeaderField:@"Accept"];
[request addValue:@"Application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"Basic %@",USER_PWD] forHTTPHeaderField:@"Authorization"];

I reference the link How to make an HTTP request in Swift?, and it only show the following code:

let url = NSURL(string: "http://www.stackoverflow.com")
let request = NSURLRequest(URL: url)

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

But it didn't select Get or Post , and it also didn't set the header. How to decide the Http method is Get or Post? and how to set the Header? in Swift

Thanks in advance.

Community
  • 1
  • 1
Martin
  • 2,813
  • 11
  • 43
  • 66

1 Answers1

16
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22