Swift - Send json body through the still

Asked

Viewed 115 times

0

I’m on my first project on Swift, and I need to send a RequestBody in the POST by Alamofire.

This is an example of JSON I need to send:

{
"user":{
    "email":"[email protected]"
 }
}

And I have my Class User.

 func toDictionary() -> [String:Any]
{
    var dictionary = [String:Any]()

    if email != nil{
        dictionary["email"] = email
    }

    return dictionary
}

I am using the SwiftyJSON to do the parse. My question is basically how I can transform my object, in this JSON structure that I need and send by Alamofire as POST.

1 answer

0

Alamofire allows sending a dictionary as parameters of a POST:

Alamofire.request(.POST, "http://myserver.com", parameters: User.toDictionary(), encoding: .JSON)
.responseJSON { request, response, JSON, error in
    print(response)
    print(JSON)
    print(error)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.