0

We need to upload an image to an API with POST method, parameters and a token.

Here is our code:

 func myImageUploadRequest()
{
    var boole : String!
    if(card) {
        boole = "1"
    }
    else {
        boole = "0"
    }
    let headers: HTTPHeaders = ["Authorization": "Token \(token!)"]
    print(headers.debugDescription)

    let param = [
        "comment":comment,
        "category":String(category),
        "amount":String(money),
        "payment_card":boole
    ]
    Alamofire.upload(method:.post,"https://llegoelbigotes.ubiqme.es/api/new-ticket/",headers:headers, multipartFormData: {
        multipartFormData in
        if let imageData = UIImageJPEGRepresentation(image,0.6) {
            multipartFormData.appendBodyPart(data:imageData,name:"image",fileName:"file.png",mimeType: "image/png")
        }
        for(key,value) in param {
            multipartFormData.appendBodyPart(data:value.dataUsingEncoding(NSUTF8StringEncoding)!,name:key)
        }
    }, encdingCompletion: {
            encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            print("Success")
        upload.responseJSON{ response in
            print(response.request)
            print(response.response)
            print(response.data)
            print(response.result)
        if let JSON = response.result.value {
        print("JSON: \(JSON)")
        }
    }
        case .Failure(let encodingError):
        print(encodingError)
        }
    })


}

This code give us an error:

Ambiguous reference to member 'upload(_:to:method:headers:)'

Any idea of what is going wrong?

1 Answer 1

1

Think you got the order of the parameters wrong.

 Alamofire.upload(multipartFormData: { multipartFormData in
            // multipartFormData here
        },
        to: url,
        method: .post,
        headers: headers) { encodingResult in
            // results here
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.