9

Updated Alamofire 4.0.0 does not mention how to put Httpmethod & Httpheaders in upload with multipartFormData. That's why I google and found solution in that stackoverflow question. But the problem is I did same as that answer then got following error message and building is failed. Please help me how to solve it.

Type of expression is ambiguous without more context

Here is my coding:

let URL = try! URLRequest(url: Config.imageUploadURL, method: .post, headers: headers)

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(self.imageData, withName: "image", fileName: "file.png", mimeType: "image/png")
    },
    to: URL,
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                if((response.result.value) != nil) {

                } else {

                }
            }
        case .failure( _):

        }
    }
)
PPShein
  • 13,309
  • 42
  • 142
  • 227
  • Hello, what is Config.imageUploadURL? How do you make urlRequestConvertible in Alamofire 4 ? – Sam Nov 02 '16 at 16:42
  • 1
    @Sam read below answer I marked as correct answer. If not clear yet, make a question and i'll answer for you. – PPShein Nov 03 '16 at 03:27

3 Answers3

17

Alamofire.upload(multipartFormData:to:encodingCompletion:) takes a URLConvertible for the to: argument. Instead, you should use Alamofire.upload(multipartFormData:with:encodingCompletion:) which takes a URLRequestConvertible for its with: argument.

I think your argument name of URL that is the same as the type URL() helps in creating strange compiler errors.

The following compiles for me:

let url = try! URLRequest(url: URL(string:"www.google.com")!, method: .post, headers: nil)

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(Data(), withName: "image", fileName: "file.png", mimeType: "image/png")
    },
    with: url,
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                if((response.result.value) != nil) {

                } else {

                }
            }
        case .failure( _):
            break
        }
    }
)
Jon Brooks
  • 2,472
  • 24
  • 32
1

For me the build error was caused by a multipartFormData.appendBodyData(). After replacing it with multipartFormData.append() the problem was solved.

Florian Blum
  • 648
  • 6
  • 16
1

I got the same error, after spending lots of time, i found that issue was:

I was passing MutableURLRequest instead of passing URLRequest object. Thats why i were getting this error. After type casting it to URLRequest, it start working.

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81