0

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":"examplemail@gmail.com", "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

Dimitri T
  • 921
  • 1
  • 13
  • 27
  • what is your `print (response)` output? it should be from there just get the value by key – xmhafiz Jul 16 '16 at 23:29
  • 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 Jul 16 '16 at 23:35
  • sorry, i mean `print(response.result.value`) or `print(data)` – xmhafiz Jul 16 '16 at 23:39
  • 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 Jul 16 '16 at 23:47
  • PS when i added print(data) to the code, it now returns status 406 – Dimitri T Jul 16 '16 at 23:50
  • OK I got the status 406 unauthorized as I was trying the same user registration. I have updated the user registration to a new registration. I get the 200 status response, and this : } }) Optional(<207b2275 6964223a 22333738 222c2275 7269223a 22687474 70733a2f 2f777777 2e63696e 656d6d65 7273652e 636f6d2f 696f7331 2f757365 722f3337 38227d>) – Dimitri T Jul 16 '16 at 23:55
  • just added my answer. what is the `print(response.result.value) ` ? – xmhafiz Jul 17 '16 at 00:11
  • I'm getting a red alert error message 'value of type NSHTTPURLResponse?' has no member result' when I add the line print(response.result.value) – Dimitri T Jul 17 '16 at 02:24

2 Answers2

1

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 
    }

}
Syafiq Mastor
  • 178
  • 2
  • 10
xmhafiz
  • 3,482
  • 1
  • 18
  • 26
  • hi cod3rite, thank you this sounds promising... I'm getting a red alert error message which says "value of type NSHTTPURLResponse?' has no member result' on the line "if let JSON = response.result.value {". Any ideas? – Dimitri T Jul 17 '16 at 02:19
  • Thanks so much. I am now getting a red alert on "if let JSON = response.result.value {" saying 'value of tuple type '(NSURLRequest?, NSHTTPURLResponse?, Result)' (aka 'Optional, Result)') has no member 'result'. Must be close! Any thoughts? – Dimitri T Jul 17 '16 at 04:11
  • sorry for late. could I know your alamofire version? – xmhafiz Jul 17 '16 at 14:47
  • no worries and thank you. I do very much appreciate your help here. I'm using cocoa pods with Alamofire 2.0.2 and swiftyJSON – Dimitri T Jul 18 '16 at 01:15
  • I see, maybe you can check the different here. hope it helps http://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift – xmhafiz Jul 18 '16 at 01:17
  • @DimitriT I have edited the post. Please try and let me know if it works – Syafiq Mastor Jul 18 '16 at 01:36
  • thank you @SyafiqMastor. I enter that code exactly but it does not show 'Alamofire' in blue text as you had it, and on the line 'switch response result' I am getting a red alert 'Value of tuple type 'NSURLRequest?, NSHTTPURLResponse Result'(aka 'Optional' – Dimitri T Jul 18 '16 at 04:28
  • Have you use swiftyJSON as mentioned? – Syafiq Mastor Jul 18 '16 at 04:30
  • yes, I have at the top of the file: 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") } and then the code as I originally wrote, but with your replacement code from the line beginning 'Alamofire.request'... – Dimitri T Jul 18 '16 at 04:47
  • I have updated my original post to include the code beforehand (including import Alamofire) to clarify and give context, in case it is of help. – Dimitri T Jul 18 '16 at 04:57
1

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.

Kyle Browning
  • 5,374
  • 1
  • 19
  • 30
  • thank you H44f33z and Kyle, it worked. confirming the working code is: Alamofire.request(.POST, voteEndpoint, parameters: newVote, encoding: .JSON) .responseJSON { response in if let JSON = response.result.value { // JSON is your sever response print("JSON: \(JSON)") // you get the uid from this let name = JSON["uid"] as! String – Dimitri T Jul 28 '16 at 06:27
  • Hi Kyle and helpful people, I am using this same code in my Xcode but getting an error message on the line 'if let JSON = response.result.value', the error says 'value of type NSHTTPURL response? has no member 'result'. I've tried updating the Alamofire to 3.0, same error. I was finding this error coming up a lot before- am wondering if there is something about where the code is placed, or what could be bringing this? Would very much appreciate your thoughts. I have Base64credentials added before the API. – Dimitri T Sep 25 '16 at 06:00