如何使用AFNetworking进行POST并将参数作为POST数据发送?

时间:2015-11-03 22:40:14

标签: ios swift post afnetworking instagram

我试图在Instagram API上发布POST来改变用户的关系但我却无法接听电话。我做了一些研究,一个人说要将action=follow部分放在POST Data中。我进一步搜索了这个,仍然无法通过我的请求。

let request = NSMutableURLRequest(URL: NSURL(string: "https://api.instagram.com/v1/users/\(user.id)/relationship?access_token=\(account.accessToken)")!)
        let key = "action=follow"
        let mastData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!
        let mastLength = "\(mastData.length)"

        request.HTTPMethod = "POST"
        request.HTTPBody = mastData
        request.addValue(mastLength, forHTTPHeaderField: "Content-Length")
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

        let operation = AFHTTPRequestOperation(request: request)
        operation.responseSerializer = AFJSONResponseSerializer()
        operation.setCompletionBlockWithSuccess({ (_, result) -> Void in
                        let result = result as! NSDictionary
                        let data = result["data"] as! NSDictionary

                        completion(true)
            }) { (_, error) -> Void in
                completion(false)
        }

此外,我知道user.id有效且account.accessToken也有效,因为我已在应用的其他部分成功使用它们。

0 个答案:

没有答案