HTTPost and HTTPGet Request in Swift 1.2 / 2

+1 Somnath Guthula · September 28, 2015
Hello ,

I am trying to build an app in XCode 7 and have to make a network call (HTTPPost and HTTPGet) ? What is the best way to make the call natively without using any external libraries.

How to add paramaters for POST Request.

I had tried using the following code.


let url = NSURL(string: "https://randomuser.me/api/")
            let session = NSURLSession.sharedSession()
            
            let dataTask = session.dataTaskWithURL(url!, completionHandler: { (data: NSData?, response:NSURLResponse?,error: NSError?) -> Void in
                //do something
                print(data)
                print(response)
                print(error)
            })
            
            dataTask.resume()


Output: 

Optional(<7b0a2020 20202272 6573756c 7473223a 205b0a20 20202020 2020207b 0a202020 20202020 20202020 20227573 6572223a 207b0a20 20202020 20202020 20202020 20202022 67656e64 6572223a 20226665 6d616c65 222c0a20 20202020 20202020 20202020 20202022 6e616d65 223a207b 0a202020 20202020 20202020 20202020 20202020 20227469 746c6522 3a20226d 7273222c 0a202020 20202020 20202020 20202020 20202020 20226669 72737422 3a202261 6c6c6567 6f6e6461 222c0a20 20202020 20202020 20202020 20202020 20202022 6c617374 223a2022 64696b73 220a2020 20202020 20202020 20202020 20207d2c 0a202020 20202020 20202020 20202020 20226c6f 63617469 6f6e223a 207b0a20 20202020 20202020 20202020 20202020 20202022 73747265 6574223a 20223130 3832206c 65696473 656b6164 65222c0a 20202020 20202020 20202020 20202020 20202020 22636974 79223a20 22746579 6c696e67 656e222c 0a202020 20202020 20202020 20202020 20202020 20227374 61746522 3a202266 72696573 6c616e64 222c0a20 20202020 20202020 20202020 20202020 20202022 7a697022 3a203730 3732370a 20202020 20202020 20202020 20202020 7d2c0a20 20202020 20202020 20202020 20202022 656d6169 6c223a20 22616c6c 65676f6e 64612e64 696b7340 6578616d 706c652e 636f6d22 2c0a2020 20202020 20202020 20202020 20202275 7365726e 616d6522 3a202279 656c6c6f 776d6f75 73653633 37222c0a 20202020 20202020 20202020 20202020 22706173 73776f72 64223a20 226b6f6e 67222c0a 20202020 20202020 20202020 20202020 2273616c 74223a20 22364762 68494662 65222c0a 20202020 20202020 20202020 20202020 226d6435 223a2022 36613365 61353164 30326663 63656335 38626161 31623031 65373134 37303366 222c0a20 20202020 20202020 20202020 20202022 73686131 223a2022 31363639 31363631 66346661 38386338 65636161 64303765 63653364 30343761 32326435 61653535 222c0a20 20202020 20202020 20202020 20202022 73686132 3536223a 20226663 35393065 39656261 64626661 35313661 64633036 62653032 64653463 36613930 35306336 63313861 63396630 38353662 33323733 61343239 30316338 3764222c 0a202020 20202020 20202020 20202020 20227265 67697374 65726564 223a2039 39303930 34383437 2c0a2020 20202020 20202020 20202020 20202264 6f62223a 20363730 30383035 34352c0a 20202020 20202020 20202020 20202020 2270686f 6e65223a 20222837 3434292d 3634372d 38323032 222c0a20 20202020 20202020 20202020 20202022 63656c6c 223a2022 28393631 292d3832 302d3331 3832222c 0a202020 20202020 20202020 20202020 20224253 4e223a20 22383532 35313737 35222c0a 20202020 20202020 20202020 20202020 22706963 74757265 223a207b 0a202020 20202020 20202020 20202020 20202020 20226c61 72676522 3a202268 74747073 3a2f2f72 616e646f 6d757365 722e6d65 2f617069 2f706f72 74726169 74732f77 6f6d656e 2f36392e 6a706722 2c0a2020 20202020 20202020 20202020 20202020 2020226d 65646975 6d223a20 22687474 70733a2f 2f72616e 646f6d75 7365722e 6d652f61 70692f70 6f727472 61697473 2f6d6564 2f776f6d 656e2f36 392e6a70 67222c0a 20202020 20202020 20202020 20202020 20202020 22746875 6d626e61 696c223a 20226874 7470733a 2f2f7261 6e646f6d 75736572 2e6d652f 6170692f 706f7274 72616974 732f7468 756d622f 776f6d65 6e2f3639 2e6a7067 220a2020 20202020 20202020 20202020 20207d0a 20202020 20202020 20202020 7d0a2020 20202020 20207d0a 20202020 5d2c0a20 20202022 6e617469 6f6e616c 69747922 3a20224e 4c222c0a 20202020 22736565 64223a20 22313166 32626364 30613564 33316232 37303622 2c0a2020 20202276 65727369 6f6e223a 2022302e 37220a7d>)
Optional(<NSHTTPURLResponse: 0x78604c30> { URL: https://randomuser.me/api/ } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = "keep-alive";
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Mon, 28 Sep 2015 12:49:00 GMT";
    Server = nginx;
    "Strict-Transport-Security" = "max-age=31536000;";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "PHP/5.5.9-1ubuntu4.11";
} })
nil


Can someone help me get the right output with an easy understanding.

P.S I am new to iOS Development and have no prior experience with Objective C. Thanks in advance.

Post a Reply

Replies

Oldest  Newest  Rating
0 Aju Antony · November 19, 2015
Hi,

what's happening here is that you are getting all the three outputs just the way you have requested. 

Optional(...) is the output for 'print(data)', you are getting the hex representation of the response data because you haven't serialized the response yet.

if your response is in JSON format, look into NSJSONSerialization to get the understandable form of your response. look into the following documentation to know more about this process,
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/

if your response is in XML format, you can use any one of million open source projects to serialize your data.


The second Optional ( <NSHTTPURLResponse: ...) is the response header description. Output for 'print(response)'


the 'nil' at the last line is the output for 'print(error)', which means that you haven't encountered any errors during the entire process.

Hopefully that clears everything up. :)

Happy Coding :D 
  • 1

iOS Development / Swift

107,278 followers
About

This forum is for questions, comments, and general discussion about iOS Development and Swift.

Links
Moderators
Bucky Roberts Administrator