# User-Agent in URLSession

**URL:** <https://forums.kodeco.com/t/user-agent-in-urlsession/38816>\
**Category:** iOS\
**Created:** [January 23, 2018, 12:17pm UTC](https://forums.kodeco.com/t/user-agent-in-urlsession/38816 "2018-01-23T12:17:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevewebber](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/stevewebber/32/101506_2.png) [@stevewebber](https://forums.kodeco.com/u/stevewebber)\
**Post date:** [January 23, 2018, 12:17pm UTC](https://forums.kodeco.com/t/user-agent-in-urlsession/38816/1 "2018-01-23T12:17:47Z")

</div>

How does one set the User-Agent header field before initiating a URLSession? I need to write an app that looks like a request is from a macOS.

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [January 23, 2018, 4:42pm UTC](https://forums.kodeco.com/t/user-agent-in-urlsession/38816/2 "2018-01-23T16:42:24Z")

</div>

@stevewebber. Thanks very much for your question!

From whatever little I know, playing with User-Agent is never a good idea.

See [HTTPAdditionalHeaders](https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1411532-httpadditionalheaders?language=objc)

`sessionConfiguration.HTTPAdditionalHeaders = ["User-Agent": "Your User Agent"]`

I found the following [answer](https://stackoverflow.com/questions/36379347/does-nsurlsession-send-user-agent-automatically) on StackOverflow that I believe answers your question.

I also found the following [link](https://www.youtube.com/watch?v=kq_7ZYb-zKk) on YouTube that might also be of help.

🙂

I hope this helps!

All the best!

---

<div class="post-metadata">

**Author:** ![stevewebber](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/stevewebber/32/101506_2.png) [@stevewebber](https://forums.kodeco.com/u/stevewebber)\
**Post date:** [January 25, 2018, 12:56pm UTC](https://forums.kodeco.com/t/user-agent-in-urlsession/38816/3 "2018-01-25T12:56:58Z")

</div>

Apparently my problem was something else. I am trying to scan the HTML I get back from a URL but when I use a “GET” or a “POST” to read the HTML I receive some intermediate page.

The code I tried was:

```
    let sessionConfiguration = URLSessionConfiguration()
    sessionConfiguration.httpAdditionalHeaders = ["User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3"]
    let session = URLSession.shared
    let url = URL(string: "https://www.greyoakscc.com/default.aspx?p=MemProfile&id=8416150&ssid=318032&vnf=1")
    let request = NSMutableURLRequest(url: url!)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (data, response, error) -> Void in

        var usedEncoding = String.Encoding.utf8 // Some fallback value
        if let encodingName = response?.textEncodingName {
            let encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName as CFString))
            if encoding != UInt(kCFStringEncodingInvalidId) {
                usedEncoding = String.Encoding(rawValue: encoding)
            }
        }
        if let myString = NSString(data: data!, encoding: usedEncoding.rawValue) {
            print("this is my string: \(myString)")
        } else {
            print("failed to decode data")
        }
    })
    task.resume()

```

When I use the URL above in a browser and then look at the source returned it is what I want, but when I look at the HTML returned from the code above it is not. I am very puzzled.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [July 9, 2018, 4:30am UTC](https://forums.kodeco.com/t/user-agent-in-urlsession/38816/4 "2018-07-09T04:30:16Z")

</div>

Closed on July 9, 2018, 4:30am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
