title: REST API call with Swift 5.2
body: <p>I programmed a simple python script to get dividend data of a stock from Finnhub.io, this code works and is posted below.</p>
  
  <pre><code>#!/usr/bin/env python
  import requests
  
  resp = requests.get('https://finnhub.io/api/v1/stock/dividend?symbol=AAPL&amp;from=2010-01-01&amp;to=2020-12-31&amp;token=xxx')
  if resp.status_code != 200:
      # This means something went wrong.
      raise ApiError('GET /api/v1/stock/dividend?symbol=AAPL&amp;from=2010-01-01&amp;to=2020-12-31&amp;token=xxx {}'.format(resp.status_code))
  else:
      for dividend_item in resp.json():
          print('{} {} {}'.format(dividend_item['date'], dividend_item['symbol'], dividend_item['amount']))
  </code></pre>
  
  <p>The result of running the python script looks like this:</p>
  
  <pre><code>2020-02-07 AAPL 0.77
  2019-11-07 AAPL 0.77
  2019-08-09 AAPL 0.77
  2019-05-10 AAPL 0.77
  2019-02-08 AAPL 0.73
  2018-11-08 AAPL 0.73
  2018-08-10 AAPL 0.73
  2018-05-11 AAPL 0.73
  2018-02-09 AAPL 0.63
  2017-11-10 AAPL 0.63
  2017-08-10 AAPL 0.63
  2017-05-11 AAPL 0.63
  2017-02-09 AAPL 0.57
  2016-11-03 AAPL 0.57
  2016-08-04 AAPL 0.57
  2016-05-05 AAPL 0.57
  2016-02-04 AAPL 0.52
  2015-11-05 AAPL 0.52
  2015-08-06 AAPL 0.52
  2015-05-07 AAPL 0.52
  2015-02-05 AAPL 0.47
  2014-11-06 AAPL 0.47
  2014-08-07 AAPL 0.47
  2014-05-08 AAPL 0.47
  2014-02-06 AAPL 0.43571
  2013-11-06 AAPL 0.43571
  2013-08-08 AAPL 0.43571
  2013-05-09 AAPL 0.43571
  2013-02-07 AAPL 0.37857
  2012-11-07 AAPL 0.37857
  2012-08-09 AAPL 0.37857
  </code></pre>
  
  <p>What gives me a headache is converting this to Swift 5.2 on iOS 13. I found a good post here on stackoverflow (<a href="https://stackoverflow.com/questions/24321165/make-rest-api-call-in-swift">Make REST API call in Swift</a>) and adjusted the code to my below Swift code.</p>
  
  <pre><code>    func getDividendData() {
          let params = ["username":"john", "password":"123456"] as Dictionary&lt;String, String&gt;
  
          var request = URLRequest(url: URL(string: "https://finnhub.io/api/v1/stock/dividend?symbol=AAPL&amp;from=2010-01-01&amp;to=2020-12-31&amp;token=xxx")!)
          request.httpMethod = "GET"
          request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
          request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  
          let session = URLSession.shared
          let task = session.dataTask(with: request, completionHandler: { data, response, error -&gt; Void in
              print(response!)
              do {
                  let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary&lt;String, AnyObject&gt;
                  print(json)
              } catch {
                  print("error")
              }
          })
  
          task.resume()
      }
  </code></pre>
  
  <p>When I then call the <code>getDividendData()</code> method I get below errors. As I am not very experienced in Swift programming yet I hope someone could give me a hint.</p>
  
  <pre><code>2020-04-21 22:39:27.963666+0200 RESTtest[15836:1085147] GET method must not have a body
  2020-04-21 22:39:27.967193+0200 RESTtest[15836:1085147] Task &lt;C919CF75-FB01-4FF5-8CC8-646B1CA1FB5D&gt;.&lt;1&gt; finished with error [-1103] Error Domain=NSURLErrorDomain Code=-1103 "resource exceeds maximum size" UserInfo={NSUnderlyingError=0x600002953240 {Error Domain=kCFErrorDomainCFNetwork Code=-1103 "(null)"}, NSErrorFailingURLStringKey=https://finnhub.io/api/v1/stock/dividend?symbol=AAPL&amp;from=2010-01-01&amp;to=2020-12-31&amp;token=xxx0, NSErrorFailingURLKey=https://finnhub.io/api/v1/stock/dividend?symbol=AAPL&amp;from=2010-01-01&amp;to=2020-12-31&amp;token=xxx, NSLocalizedDescription=resource exceeds maximum size}
  Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/joe/Desktop/RESTtest/RESTtest/ViewController.swift, line 27
  2020-04-21 22:39:27.967483+0200 RESTtest[15836:1085147] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/joe/Desktop/RESTtest/RESTtest/ViewController.swift, line 27
  (lldb) 
  </code></pre>
  
  <p>P.S.: For privacy reasons I replaced the token with xxx.</p>