Hi Forum
I have been working on this code in Xcode PlayGrounds and it now works fint against my API, but I have quite some trouble creating a new project in Xcode and start make it paired with a button that initiate the http post. Anyone have an example on how I get this code working with a button?
just one in the middel of the screen. Also how do I get this in a new Xcode project, if I creat a new I get errors.
Thanks
Michael
I have been working on this code in Xcode PlayGrounds and it now works fint against my API, but I have quite some trouble creating a new project in Xcode and start make it paired with a button that initiate the http post. Anyone have an example on how I get this code working with a button?
just one in the middel of the screen. Also how do I get this in a new Xcode project, if I creat a new I get errors.
Code Block import SwiftUI // ********************************************** // // xs.services API code for iPhone/iPad // // ********************************************** // let session = URLSession(configuration: .default) let url = URL(string: "http:...//ServerIP:8000/xmlcommand")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("text/plain", forHTTPHeaderField: "Content-Type") // I guess this can be "text/xml" // Working line request.httpBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Commands><command type=\"open\"><name>environments/Animals.hwe</name><id>a1</id></command></Commands>".data(using: .utf8) let task = session.dataTask(with: request) { data, response, error in // print(data as Any) // print(response as Any) // print(error as Any) // do something with the result print(data as Any? as Any) if let data = data { print(String(data: data, encoding: .utf8)!) } else { print("no data") } } task.resume() // <- otherwise your network request won't be started
Thanks
Michael
What errors do you get when you create a new project?
What system are you using?
You can do the following to setup a button to send a request.
Note: you need to use "https" otherwise you will have to fiddle with the security entitlements.
What system are you using?
You can do the following to setup a button to send a request.
Note: you need to use "https" otherwise you will have to fiddle with the security entitlements.
Code Block import SwiftUI struct ContentView: View { var body: some View { Button(action: {sendHttpPost()}) { Text("Press here to send a post") .padding(15) .foregroundColor(.blue) .border(Color.blue, width: 2) }.buttonStyle(PlainButtonStyle()) } func sendHttpPost() { let session = URLSession(configuration: .default) let url = URL(string: "http:...//ServerIP:8000/xmlcommand")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("text/plain", forHTTPHeaderField: "Content-Type") // I guess this can be "text/xml" // Working line request.httpBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Commands><command type=\"open\"><name>environments/Animals.hwe</name><id>a1</id></command></Commands>".data(using: .utf8) let task = session.dataTask(with: request) { data, response, error in // print(data as Any) // print(response as Any) // print(error as Any) // do something with the result print(data as Any? as Any) if let data = data { print(String(data: data, encoding: .utf8)!) } else { print("no data") } } task.resume() // <- otherwise your network request won't be started } }