3

I managed to fetch json from my server but now I want to add extra security by way of http headers. This is how my code barely looks like for now:

let urlPath = "http://www.xxxxxxxx.com"
        let url = NSURL(string: urlPath)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
            if ((error) != nil) {
                println("Error")
            } else {
                // process json
                let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

                println(jsonResult["user"])
            }
        })

The headers that I want to add to this request are the following:

  • uid, which holds an integer value
  • hash, which is a string

If it helps, I have another app built in Titanium framework which uses this syntax:

xhr.setRequestHeader('uid', userid);
xhr.setRequestHeader('hash', hash);

So, am basically looking for a Swift equivalent.

6
  • possible duplicate of NSURLSessionConfiguration HTTPAdditionalHeaders not set Commented Sep 25, 2015 at 16:31
  • @Abizern how can that question be a duplicate for this question? That one is for objective-c isn't ? Commented Sep 25, 2015 at 16:42
  • @WilliamKinaan the answer is in Swift and answers this question. Commented Sep 25, 2015 at 16:44
  • @Abizern that doesn't make any sense. I ask in Jave and a question comes in Python. POOM accepted answer Commented Sep 25, 2015 at 16:45
  • Did you check my answer please ? Commented Sep 26, 2015 at 11:11

2 Answers 2

4

You are using dataTaskWithURL while you should use dataTaskWithRequest, that takes NSMutableURLRequest object as an input. Using this object you can set HTTP headers, HTTPBody, or HTTPMethod

        let urlPath = "http://www.xxxxxxxx.com"
        let url = NSURL(string: urlPath)
        let session = NSURLSession.sharedSession()
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "GET" // make it post if you want
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")//This is just an example, put the Content-Type that suites you
        //request.addValue(userid, forHTTPHeaderField: "uid")
        //request.addValue(hash, forHTTPHeaderField: "hash")
        let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
          //do anything you want
        })
        task.resume()
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest you to use Alamofire for networking https://github.com/Alamofire/Alamofire

It is written in Swift and is every easy to use. Have a look at that page.

3 Comments

Actually i do not see the reason of your comment. Never said it was more powerful, i just found it easier to use, so i suggested to use it.
what is the reason of my comment? bty it is not me who downvotes, and I am checking the library, it is nice, but i always prefer to go with native
It's a cool library and well supported by the community. Give it a try next time you need networking 😀

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.