0

How to solve this issue

Ambiguous reference to member 'dataTask(with:completionHandler:)'

    guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }

    let request = NSMutableURLRequest(url: requestUrl)
    request.httpMethod = "POST"

    let postString = "firstName=James&lastName=Bond"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request){ (data, response, error) in

        if error != nil
        {
            //print(error)
            return
        }

        //You can print out response object
        print("response = \(response)")

        //Print out response body
        let responseString = String(data: data, encoding: NSUTF8StringEncoding)
        print("response data = \(responseString)")

        var err: Error?
        var json = JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary

        if let parseJSON = json {
            var firstNameValue = parseJSON["firstName"] as? String
            print("first name value = \(firstNameValue)")
        }
    }
    task.resume()
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Bar Uncle
  • 1
  • 3

1 Answers1

0

Call should be like this , first parameter is of type URL not NSMutableURLRequest

  guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }

    var  request = URLRequest(url: requestUrl)
    request.httpMethod = "POST"

    let postString = "firstName=James&lastName=Bond"
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with: request){ (data, response, error) in

        if error != nil
        {
            //print(error)
            return
        }


    }
    task.resume()

see here signature of available methods enter image description here

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87