Catch the return of an HTTP POST (IOS SWIFT)

0

I need to do an HTTP POST for a page that simply returns a literal (which can be "OK" or "ERROR") I have a tutorial from WEB that shows how to do this and it returns a JSON, I followed the tutorial and it works perfectly.

My problem is exactly there ... it returns a JSON and what my site returns is not a JSON but a simple literal ..

How do I get this literal that the site is returning ... I have tried several changes in the code below but without success.

let parameters = ["username": "@kilo_loco", "tweet": "HelloWorld"]

    guard let url = URL(string: "http://xxxxxxxxxx.com/ola.php") else { return }
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    //request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
    request.httpBody = httpBody

    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in
        if let response = response {
            print ("*******")
            print(response)
        }

        //print(data as Any)

        if let data = data {
            print("** TESTE **")
           print(data)
            //do {
              //  let json = try JSONSerialization.jsonObject(with: data, options: [])
                //print(json)
            //} catch {
              //  print(error)
           // }
        }

    }.resume()

The last "Print (data) returns me the size in bytes and not the contents of the literal ..

    
asked by anonymous 25.07.2017 / 04:03

1 answer

0

You have to format the api return data as JSON

Example:

$return = array(
    'sucesso' => 1,
    'usuario' => $usuario
    );

echo json_encode($return);
    
26.06.2018 / 23:50