1

I tried that code found on the web, but data is always nil. But when I tap it in safari, I can see a result...

 let urlStr =  "https://gmxx2.x.frxx.com:x/xxx/xxx/FR&174"

        let url = URL(string: urlStr)
        URLSession.shared.dataTask(with: url!, completionHandler: {
            (data, response, error) in
            if(error != nil){
                print("error")
            }else{
                do{
                    let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]


                    OperationQueue.main.addOperation({

                    })

                }catch let error as NSError{
                    print(error)
                }
            }
        }).resume()

Here is the response value where it seems not to show problems:

▿ Optional<NSURLResponse>
  - some : <NSHTTPURLResponse: 0x6000004e14c0> { URL: https://www.burningmeter.com/tournaments.json?page=1 } { Status Code: 200, Headers {
    "Access-Control-Allow-Origin" =     (
        "*");
    "Cache-Control" =     (
        "max-age=0, private, must-revalidate");
    Connection =     (
        "keep-alive");
    "Content-Type" =     (
        "application/json; charset=utf-8");
    Date =     (
        "Fri, 08 Feb 2019 10:46:28 GMT" );
    Etag =     (
        "W/\"c9cf3d615d984a1392782546f941543b\"");
    Server =     (
        Cowboy);
    "Strict-Transport-Security" =     (
        "max-age=31536000");
    "Transfer-Encoding" =     (
        Identity);
    Via =     (
        "1.1 vegur");
    "X-Content-Type-Options" =     (
        nosniff);
    "X-Frame-Options" =     (
        SAMEORIGIN);
    "X-Request-Id" =     (
        "08e98d62-cf30-4028-9cd9-12668f1754ca");
    "X-Runtime" =     (
        "0.021782" );
    "X-Xss-Protection" =     (
        "1; mode=block");} }

The error value is equal to nil.

I precise that I put App Transport Security Settings to YES in info.plist

EDIT:
It works with the option to [] but I was looking only for the value of data that was equal to 0 bytes, but the json has a value so it works.
enter image description here Even with that param:
enter image description here EDIT2: But when I try with that WS: https://gmp2.newtelapps.fr:5000/guests/contacts/FR&174 it doesn't work.

The only difference I can see is the response format in safari

https://www.burningmeter.com/tournaments.json?page=1 => {...}

https://gmxx2.x.frxx.com:x/xxx/xxx/FR&174 => [{...}]

Error message: Could not cast value of type '__NSArrayI' (0x108ac6da8) to 'NSDictionary' (0x108ac5818). Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • 1
    Check the response object. You might be getting some error. The key is required if you are using http URLs – Sachin Vas Feb 08 '19 at 10:57
  • Check both response and error object. You might get more information – Jimmy James Feb 08 '19 at 10:59
  • 2
    The code is supposed to work. However print **always** the `error` instance rather than meaningless literal string `"error"`. And JSON dictionaries in Swift 3+ are `[String:Any]` and `.allowFragments` is pointless. Omit the parameter `options` – vadian Feb 08 '19 at 10:59
  • @SachinVas I updated my ticket, I see nothing special in response – ΩlostA Feb 08 '19 at 11:02
  • @JimmyJames the error value is equal to nil – ΩlostA Feb 08 '19 at 11:02
  • 1
    @ΩlostA . your code working for me – Uma Madhavi Feb 08 '19 at 11:05
  • @UmaMadhavi I updated the WS, because it worked with the first one, including a bug in the simulator with the 0 bytes value of data, but it doesn't work with that second WS... – ΩlostA Feb 08 '19 at 11:20
  • @ΩlostA. check this link https://stackoverflow.com/a/53211144/5362916 – Uma Madhavi Feb 08 '19 at 11:21
  • 1
    The error in the ***EDIT2*** `newtelapps.fr` URL occurs because the JSON is an array (`[[String : Any]]`), what the error message clearly says. – vadian Feb 08 '19 at 11:35
  • @vadian oh yes, my mistake. That one I could do an effort – ΩlostA Feb 08 '19 at 11:45
  • After edit Mar 7: Please learn to read JSON. It’s pretty easy, `[]` is array, `{}` is dictionary. And learn also to read error messages. The error tells you exactly what’s wrong – vadian Mar 07 '19 at 09:28

1 Answers1

2

I made a slight modifications on your code, I have debugged and got the response.

        let urlString = "https://www.burningmeter.com/tournaments.json?page=1"
        guard let requestUrl = URL(string:urlString) else { return }
        var request = URLRequest(url:requestUrl)
        request.httpMethod = "GET"
        let task = URLSession.shared.dataTask(with: request) {
            (data, response, error) in
            if error == nil, let usableData=data {
                let response =  try? JSONSerialization.jsonObject(with: usableData, options:[])
                print(response) //JSONSerialization
            }
        }
        task.resume()
AshokPolu
  • 645
  • 5
  • 12
  • 2
    The `URLRequest` is redundant. `GET` is the default. Passing the URL to the data task is perfectly fine. And omit the `options` parameter. `[]` is the default value. – vadian Feb 08 '19 at 11:07
  • @vadian I updated my ticket, you're right, it works with the option to [] but I encounter another pb (the debugger has a problem for me)... – ΩlostA Feb 08 '19 at 11:21