Pair this http post code with a button in Xcode

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.

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
Answered by workingdogintokyo in 644054022
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.
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
}
}
Accepted Answer
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.
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
}
}
Hi workingdogintokyo

This is excellent thanks...it makes it more clear to me how these two interacts.

I can see now why it wouldn't work and I get errors when I added my playground code into Xcode in a normal way, because
I didn't do the func, and It is not a bit more clear to me how to bind these two together thank you for the clarification.

I'm on Catalina 10.15.7 with Xcode 12.1, and as you might have discovered fairly new to Xcode and all of this coding.
but hopefully I'm improving each day ;-)

I have now tried to paste the testing code into a storyboard with swift, bit in the simulator I do not get the button.

Br
Michael

Michael,

the code is not for storyboard, it is for SwiftUI interface. You will need to keep reading on Swift projects and the differences. All the best with your initial steps.
Hi Workingdogintokyo

Absolutely perfect, you got me right on track, I was now able to make an Xcode project with this modified code in it with the
button to fire the request, unfortunate the API does not support https, but that does not matters so much as this is a videowall
controller that resides on its own VLAN / Lan, and never reaches the Internet.

Thanks a lot for your response.

Br
Michael
Pair this http post code with a button in Xcode
 
 
Q