Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire sending object as parameter

I'm having hard time doing something simple. The data I want to send is the following:

  {
    "nickname":"Rado",
    "social": {
      "data: { 
        "accesstoken":"xx",
        "applicationId":"xx",
        "userId":"xx"
      },
      "type":"whatever"
    }
  }

Currently I'm doing that:

           let params = [
                "nickname": userName,
                "social": [
                    "type": "whatever",
                    "data": [
                        "userId": accessToken.userID,
                        "accesstoken": accessToken.tokenString,
                        "applicationId": accessToken.appID
                    ]
                ]
            ]

 Alamofire.request(.POST, "url/users", parameters: params, headers: nil)
                .responseJSON { response in

}

As a response I get this:

{
 "nickname":"Rado",
 "social[data][userId]":"xx",
 "social[data][applicationId]":"xx",
 "social[data][accesstoken]":"xx",
 "social[type]":"something"
}

Any advice will be appreciated!

like image 692
Radoslav Yordanov Avatar asked Mar 23 '16 10:03

Radoslav Yordanov


1 Answers

The solution turned out to be really simple. I was missing encoding: .JSON

    Alamofire.request(.POST, "url/users", parameters: params, headers: nil, encoding: .JSON)
        .responseJSON { response in

    }
like image 91
Radoslav Yordanov Avatar answered Oct 19 '22 17:10

Radoslav Yordanov