nmdias / FeedKit

An RSS, Atom and JSON Feed parser written in Swift
MIT License
1.19k stars 173 forks source link

Additional headers #143

Closed hirecom closed 1 year ago

hirecom commented 2 years ago

Hi, when making a request can I pass in additional headers to a request URL?

nmdias commented 1 year ago

This is not possible. You can however, build your own request. Here's a quick example, provided by ChatGPT. Didn't try it, but looks good.

import Foundation
import FeedKit

// Set the url for the RSS feed
let feedUrl = URL(string: "https://www.example.com/rss")!

// Create a URLSession to handle the network request
let session = URLSession(configuration: .default)

// Create a request with the URL
var request = URLRequest(url: feedUrl)

// Add some custom HTTP headers to the request
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")

// Create a data task to fetch the RSS feed data
let task = session.dataTask(with: request) { (data, response, error) in
  // Check for errors
  if error != nil {
    print(error!)
    return
  }

  // Make sure we have data
  guard let data = data else {
    print("No data returned from the request")
    return
  }

  // Use FeedKit to parse the RSS feed data
  let parser = FeedParser(data: data)
  parser.parseAsync { (result) in
    // Check for errors
    if let error = result.error {
      print(error)
      return
    }

    // Get the parsed feed
    guard let feed = result.rssFeed else {
      print("No RSS feed found")
      return
    }

    // Print the feed title and description
    print(feed.title)
    print(feed.description)

    // Loop through the items in the feed
    for item in feed.items {
      // Print the item title and link
      print(item.title)
      print(item.link)
    }
  }
}

// Start the data task
task.resume()