0

I an using Swift with an API to get a JSON

I have tested It with the function I am trying to get it to work with and it doesn't work but when I use print statements it is like this:

let url = 
"""
https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure={"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}}
"""
print(url)
print(URL(string: url))

And the output is:

https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure={"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}}
nil

I'm not sure what is going wrong

Leo Gaunt
  • 711
  • 1
  • 8
  • 29
  • 1
    Your URL is simply broken – Grzegorz Krukowski Feb 02 '21 at 22:38
  • Yeah, try putting the url in your web browser. You've got a typo somewhere – Kyle Burns Feb 02 '21 at 22:44
  • 1
    Use `URLComponents` to construct your URL – Paulw11 Feb 02 '21 at 23:03
  • Pasting that URL into a browser gives "{"response":"Invalid structure. The structure must either be a JSON, or an Array object. Make sure you use double quotation marks in the structure.","status_code":417,"status":"Expectation Failed"}". Putting the `structure` into JSONLint.com shows one too many } at the end, but even removing that results in rejection. You should probably use `POST` rather than `GET` with such a complex input requirement. Also I think the values in the structure should be filter strings, not just the key again – Paulw11 Feb 02 '21 at 23:07
  • You have an extra `}` at the end – aheze Feb 02 '21 at 23:08
  • url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) – Rob C Feb 03 '21 at 06:26

0 Answers0