iosswiftalamofireurlrequestalamofireimage

AlamofireImage: How to downdload images with POST request


AlamofireImage seems to be supposed to request with GET method in general. But in our project, to download images we have to request with POST method, because we send access token. I have searched for the similar question in Stack Overflow, but I couldn't find enough answers. Does anyone know how to download with POST request?

The URL is as follow:

https://host_name/project_name/GetImage


Solution

  • You can use af_setImage method from AlamofireImage extension of UIImageView and pass any URLRequestConvertible parameter. For example, create URLRequest instance with Alamofire initializer:

    let urlPath = "https://host_name/project_name/GetImage"
    if var imageRequest = try? URLRequest(url: urlPath, method: .post) {
        imageRequest.addValue("token", forHTTPHeaderField: "token_field")
        imageView.af_setImage(withURLRequest: imageRequest)
    }