Antiokhos Antiokhos - 3 years ago 137
HTTP Question

can't get web page source from url in Swift

I'm currently using SwiftHTTP for getting source of url address. I am using 'get method' for getting source code of url address which is

do {
let opt = try HTTP.GET(self.my_url_address!)
opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
} catch let error {
print("got an error creating the request: \(error)")

after this code run I got this output in Xcode output screen


Status Code: 200

Headers: Content-Type: text/html
Connection: keep-alive
CF-RAY: 38391215a60e2726-FRA
Date: Mon, 24 Jul 2017 18:51:24 GMT
Vary: Accept-Encoding
X-Powered-By: ASP.NET Transfer-Encoding: Identity
Server: cloudflare-nginx
Content-Encoding: gzip
Cache-Control: private

The status code is 200 but the output is not the source code of url. How can I fix this?

Answer Source

Response is correct. I've tried requesting the website (the real one) and it works:


If you decode the BASE64 data, it will render valid HTML code.

The issue seems related to encoding. After checking the website's head tag, it states that the charset is windows-1254

String(data:, encoding: .windowsCP1254) // works. latin1, etc.

Your issue is similar to SWIFT: NSURLSession convert data to String

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download