使用Python后端的Alamofire HTTP请求

2024-04-20 06:00:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这件事上耽搁了很长时间,我需要你们的帮助。在

尝试使用AlamofirePOST一张图片上传到后端,这样我就可以使用该图片从我的服务器获得一些结果。在

我前面的代码是

import UIKit
import Alamofire

class ViewController:UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate
{


    @IBOutlet weak var myImageView: UIImageView!

    @IBAction func uploadButtonTapped(sender: AnyObject) {

        myImageUploadRequest()

    }



    @IBAction func selectPhotoButtonTapped(sender: AnyObject) {

        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary

        self.present(myPickerController, animated: true, completion: nil)

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

    {
        myImageView.image = info[UIImagePickerControllerOriginalImage] as?
        UIImage

        self.dismiss(animated: true, completion: nil)

    }



    func myImageUploadRequest()
    {
        // convert image to data object
        guard let imageData = UIImageJPEGRepresentation(myImageView.image!, 1) else {
            print("Could not get JPEG representation of UIImage")
            return
        }

        // send multipart request
        Alamofire.upload( multipartFormData: { multipartFormData in
            multipartFormData.append(imageData, withName: "imagefile", fileName: "image.jpg", mimeType: "image/jpeg")},

                          to: "http://123123123123/image",
                          encodingCompletion: { encodingResult in

                            // check the response
                            switch encodingResult {
                            case .success(let upload, _, _):
                                upload.uploadProgress { progress in
                                    // Show progress
                                }
                                upload.responseJSON { response in
                                    // Handle response
                                }
                            case .failure(let encodingError):
                                print(encodingError)
                                // Handle Error
                            }
        })
    }
    }

我的屁股是

^{pr2}$

所以,我现在在控制台中遇到了这个错误:

zechengpart[55172:9628252] [MC] Reading from private effective user settings. 2018-03-13 02:50:01.108562-0500 zechengpart[55172:9628293] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

我已经修复了关于http而不是https的infolist问题。在

当我ping服务器时,它会给我 123.123.123.123--[13/Mar/2018 02:50:05]“POST/image HTTP/1.1”400-

如何在这里调试以避免错误信息?在

请帮帮我,我已经挣扎太久了。在


Tags: inimageimportselfresponse图片postupload