Md Tariqul Islam
Md Tariqul Islam

Reputation: 2804

Image Upload with multipart and bearer token

I try to upload image in the server. and it is working in postman. But from my code its show always authentication fails.

func uploadPhoto( image: UIImage, completion: @escaping (JSON) -> ()) {


        let url = "\(AppConstant.imageUpload)"


//        let headers: HTTPHeaders
//        headers = [
//            "Authorization": "Bearer \(AppConstant.token))"
//            ]


           let headers: HTTPHeaders = [
                "Content-type": "multipart/form-data",
                "Content-Disposition" : "form-data",
                "Authorization": "Bearer \(AppConstant.token))"
           ]


//       let headers: HTTPHeaders = [
//                      "Content-type": "multipart/form-data",
//                      "Content-Disposition" : "form-data",
//                      "Authorization": "Bearer \(AppConstant.token))"
//                 ]
//

//
//     let headers: HTTPHeaders = [
//                   "Content-Type": "application/json",
//                   "Content-Disposition" : "form-data",
//                   "Authorization": "Bearer \(AppConstant.token))"
//               ]


//        let headers: HTTPHeaders = [
//                   "Content-type": "application/form-data",
//                   "Authorization": "Bearer \(AppConstant.token))"
//               ]
//        let headers: HTTPHeaders = [
//                          "Content-Type": "application/json",
//                         "Authorization": "Bearer \(AppConstant.token))"
//                     ]
//
        print("token  \(AppConstant.token)    url:  \(url)")


       // let httpHeaders = HTTPHeaders(headers)
        AF.upload(multipartFormData: { multiPart in

           multiPart.append(image.jpegData(compressionQuality: 0.4)!, withName: "image", fileName: "file.jpg", mimeType: "image/jpg")
        }, to: url, method: .post, headers: headers) .uploadProgress(queue: .main, closure: { progress in
            print("Upload Progress: \(progress.fractionCompleted)")
        }).responseString(completionHandler: { data in
            print("upload finished: \(data.data)")
        }).response { (response) in
            print("upload responce:  \(response.data)")

            switch response.result {
            case .success(let resut):

                DispatchQueue.main.async {
                                 do{


                                 //   self.imageUrl = try JSONDecoder().decode(ImageUploadUrl.self, from: resut!)

                                    if let data = resut,
                                           let urlContent = NSString(data: data, encoding: String.Encoding.ascii.rawValue) {
                                           print("urlContent  \(urlContent)")
                                       } else {
                                           print("Error: ")
                                       }
                                    print("upload 10 \(self.imageUrl?.url)")


                                               }catch {
                                    print("error occur ")
                                 }
                             }
                print("upload success result: \(resut)")
            case .failure(let err):
                print("upload err: \(err)")
            }
        }
    }

enter image description here

Upvotes: 3

Views: 1384

Answers (1)

Emre Kayaoglu
Emre Kayaoglu

Reputation: 601

I found error of your typing in your header: you wrote your header as following:

let headers: HTTPHeaders = [
       "Content-type": "multipart/form-data",
       "Content-Disposition" : "form-data",
       "Authorization": "Bearer \(AppConstant.token))"
     ]

But you have to remove bracket at the last of token, so your code should be like this

let headers: HTTPHeaders = [
       "Content-type": "multipart/form-data",
       "Content-Disposition" : "form-data",
       "Authorization": "Bearer \(AppConstant.token)"
     ]

Hope to helptul

Upvotes: 3

Related Questions