How to get back an HTTP POST (IOS SWIFT)

Asked

Viewed 184 times

0

I need to do an HTTP POST for a page that simply returns a literal (which can be "OK" or "ERROR") I’ve missed one tutorial from the WEB that shows how to do this and it returns a JSON, 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’ve 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(date) returns me the size in bytes and not the content of the literal..

1 answer

0

You want to format the return data of the api as JSON

Example:

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

echo json_encode($return);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.