0
votes

How can I return the user ID from my Drupal 7 REST services after my IOS swift Xcode7 API registers a user to my site? The console returns a status 200 (OK) but not the UID registered, which I will need for further functions. What further lines of code do I need to include here, or can I add as a separate API?

Here is my code:

 import UIKit
 import Foundation
 import Alamofire
 import SwiftyJSON

class submitVoteViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    // Do any additional setup after loading the view.
    print("loaded page1")
}

@IBAction func register(sender: AnyObject) {

    // register user

    let voteEndpoint: String = "https://www.example.com/ios1/user/register.json"
    let newVote = ["account":["name":"example name", "mail":"[email protected]", "pass":"examplepass", "status":1]]
    Alamofire.request(.POST, voteEndpoint, parameters: newVote, encoding: .JSON)
        .response { request, response, data, error in
            print(request)
            print (response)
            print (error)
    }

}

When i look it up, the Drupal site shows me the UID is 375 (blocked, though I had entered status 1 so that the user would be active?) . The console response is;

loaded page1 Optional( { URL: https://www.example.com/ios1/user/register.json }) Optional( { URL: https://www.example.com/ios1/user/register.json } { status code: 200, headers { "Cache-Control" = "no-cache, must-revalidate"; Connection = "Keep-Alive"; "Content-Type" = "application/json"; Date = "Sat, 16 Jul 2016 23:03:37 GMT"; Expires = "Sun, 19 Nov 1978 05:00:00 GMT"; "Keep-Alive" = "timeout=10, max=200"; Server = Apache; "Set-Cookie" = "DRUPAL_UID=0; expires=Fri, 15-Jul-2016 23:03:37 GMT; Max-Age=-86401; path=/; domain=.example.com; secure, DRUPAL_UID=-1; expires=Tue, 09-Aug-2016 02:36:57 GMT; Max-Age=1999999; path=/; domain=.example.com; secure"; "Transfer-Encoding" = Identity; "X-Content-Type-Options" = nosniff; "X-Powered-By" = "PHP/5.6.9"; } }) nil

2
what is your print (response) output? it should be from there just get the value by key - xmhafiz
hi cod3rite, thank you for the suggestion. I have added the console response above. I see in it that it notes ""Set-Cookie"=DRUPAL_UID=0", and then DRUPAL_UID=-1, but looking up the user in the drupal site shows the UID of 375, blocked, though I had entered status as 1 for active. Any ideas? - Dimitri T
sorry, i mean print(response.result.value) or print(data) - xmhafiz
i added print(data) to the code. In addition to the same console message returned, it adds a lot of numbers (too many characters for me to add here in comment), starting with; Optional(<207b2266 6f726d5f 6572726f 7273223a 7b226e61 6d65223a 22546865 206e616d 65203c65 6d20636c 6173733d 5c22706c 61636568 6f6c6465 725c223e 61706934 3c2f656d 3e206973 20616c72 65616479 2074616b 656e2e22 - Dimitri T
PS when i added print(data) to the code, it now returns status 406 - Dimitri T

2 Answers

1
votes

You could handle the response using the response.result with SwiftyJSON. your code will be as follow.

Alamofire.request(.POST, voteEndpoint, parameters: newVote)
    .responseJSON { (response) in
        switch response.result {
            case .Success(let value) :
            let swiftyJSON = JSON(value)
            print(swiftyJSON)

            // let say your output is {"uid": "1000", "name": "bob"}
            // you could get the uid from this
            let name = swiftyJSON["uid"].stringValue 
    }

}
1
votes

According to the response from services,

HTTP/1.1 200 OK
Date: Sat, 23 Jul 2016 18:12:06 GMT
Server: Apache/2.4.17 (Unix) OpenSSL/1.0.1h mod_fcgid/2.3.9
X-Powered-By: PHP/5.6.19
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
X-Content-Type-Options: nosniff
Vary: Accept
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json

{"uid":"3","uri":"http://drupal-7-43.dd/Api/v1/user/3"}

So the response object should have a uid attached to it.

H44f33z's answer is correct.