我在我的应用程序中使用 swift
我正在尝试使用 Alamofire 发布关系(关注)与范围(“follower_list+relationships”)
注意:我正在使用沙盒用户进行测试
用这段代码
let userid = "testAccount"
let urlString = "https://api.instagram.com/v1/users/\(userid)/relationship?access_token=\(INSTAGRAM_IDS.INSTAGRAM_Token)&action=follow"
Alamofire.request(urlString).responseJSON { response in switch response.result {
case .success(let JSON):
let response = JSON as! NSDictionary
let userModel = response
print("SUCCESS")
print(userModel)
case .failure(let error):
print("ERROR")
print(error)
}
}
也试过这个
let userid = "testAccount"
let urlString = "https://api.instagram.com/v1/users/\(userid)/relationship?access_token=\(INSTAGRAM_IDS.INSTAGRAM_Token)"
let parameters: Parameters = ["action": "follow"]
Alamofire.request(urlString, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { response in switch response.result {
case .success(let JSON):
let response = JSON as! NSDictionary
let userModel = response
print("SUCCESS")
print(userModel)
case .failure(let error):
print("ERROR")
print(error)
}
}
但仍然收到此消息??
ERROR responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
有什么想法吗??
最佳答案
如果您尝试发布 json,我怀疑您需要使用 encoding: JSONEncoding.default
更改编码
The JSONEncoding type creates a JSON representation of the parameters object, which is set as the HTTP body of the request. The Content-Type HTTP header field of an encoded request is set to application/json.
在 github 上的使用
关于ios Instagram api POST 关系端点使用 Alamofire - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643107/