Read and write data with CocoaOneP and Swift


#1

Hi,

i’m trying to make a simple demo application that writes and reads some data ports; I use Xcode 7.2 with Swift.

This is the code:

let buzzerRID = EXORpcResourceID(byRID: "datasource_rid")
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let domain = NSURL(string: "https://bitandcloud.exosite.com")
let rpc = EXORpc(domain: domain, sessionConfiguration: sessionConfig)
let authKey = EXORpcAuthKey(CIK: "device_cik")
let val: NSNumber = 1

var req = EXORpcWriteRequest.writeWithRID(buzzerRID, number: val) { (error: NSError?) -> Void in
            if ((error) != nil){
                print(error)
            }
        }

rpc!.doRPCwithAuth(authKey!, requests: [req], complete: { (error: NSError?) -> Void in
            if ((error) != nil){
                print(error)
            }
        })

The error i get is:

Optional(Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={NSUnderlyingError=0x7fd481ed92a0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fd481eda9b0> { URL: https://bitandcloud.exosite.com/api:v1/rpc/process } { status code: 400, headers {
    "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    Connection = "keep-alive";
    "Content-Length" = 17;
    "Content-Type" = "text/html";
    Date = "Fri, 22 Jan 2016 14:23:32 GMT";
    Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
    Pragma = "no-cache";
    Server = Apache;
    "Set-Cookie" = "exosite=52gb95dceh8igprsnic9hkj4a1; path=/; secure; HttpOnly, exosite=u43lhom7jcqkj8ag1m43brla55; path=/; secure; HttpOnly";
} }, NSErrorFailingURLKey=https://bitandcloud.exosite.com/api:v1/rpc/process, com.alamofire.serialization.response.error.data=<48545450 20537461 7475733a 20343030 0a>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}}, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fd481eda9b0> { URL: https://bitandcloud.exosite.com/api:v1/rpc/process } { status code: 400, headers {
    "Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
    Connection = "keep-alive";
    "Content-Length" = 17;
    "Content-Type" = "text/html";
    Date = "Fri, 22 Jan 2016 14:23:32 GMT";
    Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
    Pragma = "no-cache";
    Server = Apache;
    "Set-Cookie" = "exosite=52gb95dceh8igprsnic9hkj4a1; path=/; secure; HttpOnly, exosite=u43lhom7jcqkj8ag1m43brla55; path=/; secure; HttpOnly";
} }, NSErrorFailingURLKey=https://bitandcloud.exosite.com/api:v1/rpc/process, com.alamofire.serialization.response.error.data=<48545450 20537461 7475733a 20343030 0a>, NSLocalizedDescription=Request failed: bad request (400)})

Any idea?

Thanks
Giacomo


#2

Hi Giacomo, thanks for posting!

For the JSON RPC, the content-type always must be defined as application/json; charset=utf-8 . It looks like your request content-type is html/text, which is being rejected as an unacceptable request. If you specify the content-type as application/json; charset=utf-8 , you should be set.

Let me know if I can help further!
Michelle


#3

Hi Michelle!

I’ve added this after session initialisation but I get the same error

let headers = [“Content-Type”: “application/json”, “Accept-Charset”: “utf-8”]
sessionConfig.HTTPAdditionalHeaders = headers


#4

Can you try again with only one string, “application/json; charset=utf-8” as the content-type header?
I can look into this a little further as well if that isn’t the issue.


#5

I’ve tried with one string (application/json; charset=utf-8) but I got the same error.


#6

@giacomo.pastore

I think I just noticed the actual source of your problem. You have the domain set to “https://bitandcloud.exosite.com”, but the RPC API is actually at "*.m2.exosite.com. You’ll want to set your domain to “https://bitandcloud.m2.exosite.com”.

Let me know if that gets it to work.


#7

oh right! Now it works…

thanks Patrick


#8

Wonderful! Glad to help.