设置与Instagram api一样

时间:2017-02-16 18:09:42

标签: ios swift api instagram alamofire

我正在使用Instagram应用程序构建ios应用程序,我想在帖子上设置

curl -F 'access_token=ACCESS-TOKEN' \
https://api.instagram.com/v1/media/{media-id}/likes

如何在swift中使用Alamofire进行此卷曲请求?

1 个答案:

答案 0 :(得分:1)

来自Alamofire tutorial on Github:

以下是如何使用HTTP标头创建POST请求:

let headers: HTTPHeaders = [
    "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
    "Accept": "application/json"
]

Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in
    debugPrint(response)
}

现在,为Instagram API创建一个POST:

let headers: HTTPHeaders = [
    "access_token": \(YOUR_ACCESS_TOKEN)
]

Alamofire.request("https://api.instagram.com/v1/media/\(media-id)/likes", headers: headers).responseJSON { response in
    print(response)
}

// you could also explicitly define the request as a POST
Alamofire.request("https://api.instagram.com/v1/media/\(media-id)/likes", method: .post, headers: headers)

编辑#1:

略微更改了代码以反映OP的工作解决方案。

let headers: HTTPHeaders = [
    "access_token": \(YOUR_ACCESS_TOKEN)
]

Alamofire.request("https://api.instagram.com/v1/media/\(media-id)/likes", method: .post, parameters: header).responseJSON { response in
    print(response)
}