Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add the http header "Connection :close" correctly in swift?

I have a swift app. It sends http requests to a server that I cannot access myself.

When the app is at the background for a while, and I return and try to send a request to the server I get an http error "the connection is lost".

I read this post

And I want to add connection:close header and value, as suggested.

I have tried this code:

func taskForGETMethod(method: String, pathParameters: [String], parameters: [String : AnyObject], completionHandler: (result: AnyObject!, error: NSError?) -> Void) -> NSURLSessionDataTask {

    /* 1. Build the URL and configure the request */
    let urlString = Constants.BaseURLSecure + method + HttpClient.escapedPath(pathParameters) + HttpClient.escapedParameters(parameters)
    let url = NSURL(string: urlString)!
    let request = NSURLRequest(URL: url)
    request.setValue("Connection", forKey: "close")

but all my request's are failing now. How can I add the header correctly?

like image 692
Elad Benda Avatar asked Nov 20 '25 11:11

Elad Benda


1 Answers

The problem is in line

request.setValue("Connection", forKey: "close")

The key is the title of the header, which should be "Connection" and its value should be "close". To put it simple, at the moment you are setting close = Connection and not Connection = close.

You need to change the line to

request.setValue("close", forKey: "Connection")
like image 95
o15a3d4l11s2 Avatar answered Nov 23 '25 02:11

o15a3d4l11s2