Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Cookies for URL Request

Currently I have an iOS app that pulls prices and data from websites. So far its been working well, but I want to make it more accurate. To do so, I need to set the cookies for the URL request that I'm currently using String(contentsOf: _) for.

Current Process

let requestUrl: URL = URL(string: "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple")!

var content: String?

do {
    content = try String(contentsOf: requestUrl)
} catch {
    print("Error while converting an NSURL to String: \(error)")
}

if content != "" {
    // I do things with the content of the requestUrl...
}

Could Use?

I thought that maybe I should use Alamofire instead to pull those website, and then parse the data.

I need to set the cookie that changes the store number to search, but have been unable to find a way to do so. Bellow is the code I have for pulling the websites data without setting a cookie.

let requestUrl: String = "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple"

Alamofire.request(requestUrl, method: .post).responseString { response in
    if let content: String = response.result.value {
        // I do things with the content of the requestUrl...
    }
}

Other Claims

I have found many different ways to set cookies through Alamofire that don't work, but if Alamofire isn't the way to do it, please inform me. I really need this to work, and I'm open to any and every suggestion.

like image 479
odonckers Avatar asked Mar 23 '17 21:03

odonckers


People also ask

How do I make a cookie for a specific URL?

Syntax. document. cookie = "name = yourName; path = yourPath"; Where “yourPath” is the path to the specific page on which you want to set the cookie.

Can you set cookie in GET request?

Can you set cookie in GET request? To add cookies to a request for authentication, use the header object that is passed to the get/sendRequest functions. Only the cookie name and value should be set this way.

What is a URL cookie?

An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser. The browser may store the cookie and send it back to the same server with later requests.

Does HTTP request contain cookies?

The Cookie HTTP request header contains stored HTTP cookies associated with the server (i.e. previously sent by the server with the Set-Cookie header or set in JavaScript using Document. cookie ). The Cookie header is optional and may be omitted if, for example, the browser's privacy settings block cookies.


1 Answers

It took four weeks to the day, but I figured it out! URLRequest and Alamofire were my glorious answers!

  1. Create the URL to call.

    let requestUrl: String = "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple"
    
  2. Next make the URLRequest with the URL string, and set its http method.

    var urlRequest = URLRequest(url: requestUrl)
    urlRequest.httpMethod = "POST"
    
  3. Then set the cookies for the URLRequest.

    urlRequest.setValue("myPreferredClub=4969", forHTTPHeaderField: "Cookie")
    urlRequest.httpShouldHandleCookies = true
    
  4. Finally send the URLRequest with Alamofire, and use the response data in whatever way I wish.

    Alamofire.request(urlRequest).responseString { response in
        if let content: String = response.result.value {
            // I do things with the content of the urlRequest...
        }
    }
    
like image 51
odonckers Avatar answered Oct 06 '22 01:10

odonckers