Alamofire向API“ NSInvalidArgumentException”发送请求 [英] Alamofire sending a request to an API 'NSInvalidArgumentException'

查看:87
本文介绍了Alamofire向API“ NSInvalidArgumentException”发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将图像和文本发送到api:

i am using this code to send an image and text to an api:

    func uploadImageAndData(_ url:String,parameters1:Parameters,headers1:HTTPHeaders,images:[UIImage]){
        Alamofire.upload(multipartFormData: { multipartFormData in
            // import image to request
            var i=0
            for imageData in images {
//                multipartFormData.append(self.resizeImage(image: uploadedProfileImage, targetSize: CGSize(width: 200, height: 200)
                multipartFormData.append(imageData.pngData()!, withName: "profilePic", fileName: "profileimage"+".jpeg", mimeType: "image/jpeg")
                i += 1
            }
            for (key, value) in parameters1 {
                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
            }
        } ,to: url,method:.post,
           headers:[:],

           encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (Progress) in
                    print("showuing upload progress")
                    print("Upload Progress: \(Progress.fractionCompleted)")
                    //SwiftSpinner.show(progress: Progress.fractionCompleted*100, title: "تحميل")
                })
                upload.responseJSON { response in

                    print(response.response?.statusCode as Any)

                    if let data = response.result.value, let utf8Text = String(data: data as! Data, encoding: .utf8) {
                        print("New Func Data: \(utf8Text)")
                    }
                    switch response.response?.statusCode {
                    case 201 :
                        print("Success")
                        break
                    case 413 :
                        print("Post too large")
                        break
                    case 200 :
                        print("200")
                        break
                    case 400 :
                        print("400")
                        break
                    case 401 :
                        print("401")
                        break
                    case 302 :
                        print("302")
                        break
                    case 500 :
                        print("500")
                        break
                    default: break
                    }
                }
                return
            case .failure(let encodingError): break

            }
        })
    }

我得到了这些数据:

let address: Parameters = [
          "location" : addressField.text!,
          "locationLat" :pickedLocationLat,
          "locationLong" :pickedLocationLong
     ]                           

  let body: Parameters = [
          "name": "Sample Name",
          "email": "test@test.com",
          "password": "test123",
         "address": address
    ]

并且我正在使用此代码访问该函数

and i am accessing the function using this code

var head = [String:Any]()
head["Accept"]="application/json"


uploadImageAndData(BASE_URL", parameters1: body, headers1: head as! HTTPHeaders, images: [uploadedProfileImage])

此代码抛出类似错误

Terminating app due to uncaught exception 'NSInvalidArgumentException'
reason: '-[_TtGCs26_SwiftDeferredNSDictionarySSP__$ dataUsingEncoding:]: unrecognized selector sent to instance 0x6000015bed00'
terminating with uncaught exception of type NSException

我不知道是什么导致了此问题 api 需要 json 正文,其 content-type application / json
i厌倦了删除body参数的所有内容,除了要尝试仅发送名称的名称,当我这样做时,图像成功上传,但仍然会给我这个错误,所以请帮我解决这个问题。

i dont know what is causing this problem the api requires a json body with a content-type of application/json i tired removing all the content of the body parameter except for the name to try and send only the name and when i do that the image uploads successfully but it still gives me this error,so please help me figure out this problem.

推荐答案

您需要将值设置为字符串而不是double

You need to make values a string not double

let address: Parameters = [
      "location" : addressField.text!,
      "locationLat" :"\(pickedLocationLat)",
      "locationLong" :"\(pickedLocationLong)"
]

这篇关于Alamofire向API“ NSInvalidArgumentException”发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆