making http xmlcommand to an API

Hi Forum


I'm trying to start making an ap that contains 3 - 6 buttons called

Environment1 to Environment6.


I have been reading and reading on how to do this, with xcode/swift, but i think i'm stucked.


I have a Controller API that listens to xml commands on its port http://ServIP:8000/xmlcommand/


A typical http post command will look like this: e.g issued either on a command line or via postman.


http://ServIP:8000/xmlcommand/


XML Command:


<Commands>

<command type=\"open\">

<name>environments/Environment1.hwe</name>

<id>Environment1</id>

<trans>1.0</trans>

</command>

</Commands>


Anyone have an example they could share how to create a button that actually send this XMLcode to the API when pushed?


Thank you in advance.


/Michael

Accepted Reply

Hi!


I think there are enough examples out there on how to connect a button to some method in your controller. And for executing the network request the following should lead you into the right direction:


let session = URLSession(configuration: .default)
let url = URL(string: "<destination url>")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "<xml>…</xml>".data(using: .utf8)
let task = session.dataTask(with: request) { data, response, error in
    // do something with the result
}
task.resume() // <- otherwise your network request won't be started


Regards

Christian

Replies

.

Sorry Michael,

I had a nice reply but I'm not going to wait two weeks for moderation because I accidentally used the Apple Developer obscentity "h**p://"

Hi!


I think there are enough examples out there on how to connect a button to some method in your controller. And for executing the network request the following should lead you into the right direction:


let session = URLSession(configuration: .default)
let url = URL(string: "<destination url>")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "<xml>…</xml>".data(using: .utf8)
let task = session.dataTask(with: request) { data, response, error in
    // do something with the result
}
task.resume() // <- otherwise your network request won't be started


Regards

Christian

Hi Forums

I have been trying to work with this snippet but seems like I can't get it just right. I have added an explain as I can't post an image of what I'm trying to achieve here below.

import UIKit
import SwiftUI

let session = URLSession(configuration: .default)
let url = URL(string: "http........://192.168.0.196:8000/xmlcommand")
var request = URLRequest(url: url)
request.httpMethod = "POST"
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
    // do something with the result
}
task.resume() // <- otherwise your network request won't be started




With Rested App on Mac I can make the "POST" work fine with these settings:


http. ..... ://192.168.0.196:8000/xmlcommand ........................ POST

Use Custome HTTP body - HTTPBody:

<?xml version="1.0" encoding="UTF-8"?>
<Commands>
<command type="open">
<name>environments/Animals.hwe</name>
<id>a1</id>
</command>
</Commands>



Result from Rested application that works with the XML code...

POST /xmlcommand
Response time: 24 ms


Request Headers & Body

Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: text/plain
Accept-Language: en-us

<?xml version="1.0" encoding="UTF-8"?>
<Commands>
<command type="open">
<name>environments/Animals.hwe</name>
<id>a1</id>
</command>
</Commands>

Response Headers

HTTP/1.1 200 OK
Date: Tue, 27 Oct 2020 18:03:53 GMT
Cache-Control: no-cache
Transfer-Encoding: Identity


Not sure what goes wrong in my code but seems like the XML code does not reach the api correctly.

Any ideas would be greatly appreciated.

Thanks
Michael