简体   繁体   English

如何使用 Alamofire 发出 POST 表单数据请求? - 斯威夫特

[英]How do I make POST form-data request with Alamofire? - Swift

I am trying to imitate this in Xcode using Alamofire:我正在尝试使用 Alamofire 在 Xcode 中模仿这一点: 邮递员请求的屏幕截图

Here is what I created on Swift/Xcode according to Alamofire docs :这是我根据 Alamofire 文档在 Swift/Xcode 上创建的内容:

let url = "http://127.0.0.1:8000/" /* your API url */


AF.upload(multipartFormData: { multipartFormData in
    multipartFormData.append(Data("one".utf8), withName: "file")
    
}, to: url, method: .post)
    .responseJSON { response in
        debugPrint(response)
    }

The response I get from the server is.我从服务器得到的响应是。 "Number of Files: " = 0; "文件数:" = 0; Meaning the server is not receiving the file, however it works when I do it from Postman, so what am I missing?这意味着服务器没有收到文件,但是当我从 Postman 接收文件时它可以工作,那么我错过了什么?

Also here is my django server which is taking the request, if this is needed:如果需要,这里也是我的 django 服务器,它正在接受请求:

 @csrf_exempt
 def test(request):

length = 0
try:
    length = len(request.FILES)
except Exception as e:
    print(e)
return JsonResponse({'Number of Files: ': length})
 Alamofire.upload(multipartFormData: { (multipartFormData) in
                 // For Image and other params
                       if let imageNew = image {
                           let  TimeStamp = "\(Date().timeIntervalSince1970 * 1000)"
                        if let imageData = imageNew.jpegData(compressionQuality: 0.6) {
                               multipartFormData.append(imageData, withName: imageParam , fileName: "\(TimeStamp + ".jpeg")",  mimeType: "image/jpeg")
                       }
                   }
                   for (key, value) in dict {
                       if value is String || value is Int {
                           multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
                       }
                   }
               }, to: url, method: .post, headers: headers, encodingCompletion: {
                   encodingResult in
                   switch encodingResult {
                   case .success(let upload,_,_):
                       upload.response { response in
                          // print(response)
                           
                           guard let data = response.data else { return }
                           do{
                               let userModel = try JSONDecoder().decode(T.self, from: data)
                               completion(userModel)
                           }catch let error {
                               failure(error.localizedDescription)
                               print(error.localizedDescription)
                           }
                           upload.responseJSON(completionHandler: { response in
                               print(response)
                               })
                       }
                   case .failure(_):
                       print("error")
                       break
                       //completion (nil)
                   }
               })
       }

I found the solution!我找到了解决方案! for anyone else who might be facing this problem, heres why:对于可能面临此问题的其他任何人,原因如下:

You just have to specify the filename too.您也只需要指定文件名。

AF.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(Data("one".utf8), withName: "file", fileName: "landing.jpeg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM