How to convert a CURL command to Swift

I am learning how to write my first iOS application that will request some basic OIDs from the Proliphix IP Network Thermostat. Proliphix supports several methods, such as Curl; PHP and API GET and SET. Of these methods, what would be easier in Swift?

Can someone tell me how to convert one of the following methods to Swift?

Here are examples from the Proliphix API that can be found in a Google search.


Curl

Get curl -u hostname: password --data OID1.1 = http://192.168.1.100:8100/get

Set curl -u hostname: password --data OID1.10.5 = 120 --data submit = Send      http://192.168.1.100:8100/pdp


GET API

URL used / get. A GET API request is a list of OIDs where their value is not specified. A correctly formatted request must contain the Content-Length header. The entry is an encoded authentication base word (see RFC 2617 -HTTP Authentication: Basic and Digest Access Authentication).

Inquiry

POST /get HTTP/1.1
  Authorization: Basic <credentials>
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Jakarta Commons-HttpClient/2.0.2
  Host: 192.168.111.114:8214
  Content-Length: 92
  OID1.10.9=&OID1.2=&OID1.1=&OID1.4=&OID1.8=&OID2.7.1=&

Answer

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 166

OID1.10.9=example@proliphix.com & OID1.2 = SW Dev 114 & OID1.1 = therm_rev_2 0.1.40 & OID1.4 = 192.168.111.114 & OID1.8 = 00: 11: 49: 00: 00: 58 & OID2.7.1 = NT100


API SET

URL-:/pdp. API API GET , , . -. (. RFC 2617 -HTTP Authentication: Basic Digest Access Authentication). "submit = Submit". '& "submit = Submit".

 POST /pdp HTTP/1.1
  Authorization: Basic <credentials>
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Jakarta Commons-HttpClient/2.0.2
  Host: 192.168.111.114:8214
  Content-Length: 193

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 308

PHP

PHP - , -, mod_perl. Apache - .

Get

$oids = array('OID1.4'=>'', // commonIpAddr
              'OID1.10.5'=>'',
              ‘submit’=>’Submit’); // commonCallhomeInterval
$url = "http://192.168.1.100:8100/get";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$myHeader = array("Content-Type: application/x-www-form-urlencoded" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($oids)); $response = curl_exec($ch);
curl_close($ch);
$oids = array();
parse_str($response, $oids); // converts '.' to underscore
$localip = $oids['OID1_4'];
$interval = $oids['OID1_10_5']; // in minutes
+4
1

, API, Proliphix.

, , , cURL, Swift.

API- HTTP-, NSURLSession API, Apple, , , Alamofire, .

API URL-, /get /pdp . , GET POST. API - (, OID- ), , .

, .

, NSURLSession:

if let url = NSURL(string: "http://httpbin.org/post"){
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST" //Or GET if that what you need
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")  //This is where you add your HTTP headers like Content-Type, Accept and so on
    let params = ["OID1.2" : "SW+Dev+114", "OID1.4" : "192.168.111.114"] as Dictionary<String, String> //this is where you add your parameters

    let httpData = NSKeyedArchiver.archivedDataWithRootObject(params) //you need to convert you parameters to NSData or to JSON data if the service accepts this, you might want to search for a solution on how to do this...hopefully this will get you in the right direction :-)
    request.HTTPBody = httpData
    let session = NSURLSession.sharedSession()
    session.dataTaskWithRequest(request, completionHandler: { (returnData, response, error) -> Void in
        var strData = NSString(data: returnData, encoding: NSUTF8StringEncoding)
        println("\(strData)")
    }).resume() //Remember this one or nothing will happen :-)
}

, . Google NSURLSession Alamofire, , , .

+3

All Articles