iosswiftinstagramalamofire

Set like with Instagram API


I am building an iOS app with Instagram App and I wanted to set like on a post

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

How to make this curl request using Alamofire in swift?


Solution

  • From the Alamofire tutorial on Github:

    Here's how to create a POST Request With HTTP Headers:

    let headers: HTTPHeaders = [
        "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
        "Accept": "application/json"
    ]
    
    Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in
        debugPrint(response)
    }
    

    Now, for creating a POST to the Instagram API:

    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)
    

    EDIT #1:

    Changed code slightly to reflect OP's working solution.

    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)
    }