0
votes

I have this code, I need to pass parameters by BUDDY, but I get a "rare" error. Can anybody help me? THX!!

This is the error

Optional(Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSUnderlyingError=0x7fda33f4cd80 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorCodeKey=-1, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http:.../users.php, NSErrorFailingURLKey=http:.../users.php, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=cannot parse response})

func registerUser(currentUser: Users, IsRegistered : (isRegistered: Bool, messageString: String) -> ()) {

let REGISTER_URL = "users.php?"

var bodyParams: Dictionary<String, String> = Dictionary()
bodyParams["firstName"] = currentUser.getFirstName()
bodyParams["lastName"] = currentUser.getLastName()
bodyParams["gender"] = currentUser.getGender()
bodyParams["birthday"] = currentUser.getBirthday()

let urlAppend = createFullURL(REGISTER_URL, urlParams: Dictionary())

let request = NSMutableURLRequest(URL: NSURL(string: urlAppend)!)

let loginString = "\(currentUser.getEmail()):\(currentUser.getPassword())"
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = "Basic \(loginData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength))"
let headerParams: Dictionary<String, String> = ["Authorization": base64LoginString]

for (key, value) in headerParams{
    request.setValue(value, forHTTPHeaderField: key)
}

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

//
// The problem is the following 4 lines, if I tell, there is no error, but of course, I can not register a user
//

do {

    request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(bodyParams, options: [])

} catch {

    IsRegistered(isRegistered: false, messageString: "Error al parsear los datos.")

}

request.HTTPMethod = METHOD_GET

NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in

    if (error != nil) {

        // ERROR HERE //      
        // ERROR HERE //
        // ERROR HERE //
        print(error)

    } else {

        ...
    }

    }.resume()

}
1

1 Answers

0
votes

Check this link HTTPBody

httpBody

This data is sent as the message body of the request, as in done in an HTTP POST request.

When you use request.HTTPMethod = "GET", you can not use HTTPBody to append your &k=v pairs. Add parameters to the URL string.