'Alamofire to Node.js request not being received properly on the server side?

I have a heroku app set up and an iOS client. The iOS client should be sending a post request containing some string data and an image. On the server side, I want to post the image to an S3 but I'm not sure what to do with my request.file object.

I've got an Alamofire request set up below:

func postRequest(selectedImage: UIImage) {
    let server = 'https://server.com'
    let title = 'test title name'
    let subtitle = 'test subtitle name'
            
    var parameters = [String: AnyObject]()
    parameters = ["title":title,
                  "subtitle":subtitle
        ] as [String: AnyObject]
    
    AF.upload(multipartFormData: { multiPart in
            for (key, value) in parameters {
                if let temp = value as? String {
                    multiPart.append(temp.data(using: .utf8)!, withName: key)
                }
                if let temp = value as? Int {
                    multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
                }
                if let temp = value as? NSArray {
                    temp.forEach({ element in
                        let keyObj = key + "[]"
                        if let string = element as? String {
                            multiPart.append(string.data(using: .utf8)!, withName: keyObj)
                        } else
                            if let num = element as? Int {
                                let value = "\(num)"
                                multiPart.append(value.data(using: .utf8)!, withName: keyObj)
                        }
                    })
                }
            }
        if let imageData = selectedImage.jpegData(compressionQuality: 0.9) {
            multiPart.append(imageData, withName: "cameraPhoto", fileName: "picName", mimeType: "image/jpg")
        }
        }, to: server)
            .uploadProgress(queue: .main, closure: { progress in
                //Current upload progress of file
                print("Upload Progress: \(progress.fractionCompleted)")
            })
            .responseJSON(completionHandler: { data in
                //Do what ever you want to do with response
                print("received the response")
            })
}

On the server side, I'm just using express to handle all incoming requests:

app.post('/', async function (request, response) {
  console.log(request.body) // this is all the non-image parts of the request
  console.log(request.file) // this is the image
  // in here, I want to take the image data and put the image in an S3 bucket, what AWS library command do I run to do this? Is any image data manipulation necessary?
  response.send("Hit the post")
});

I'm not sure how to handle the multi form data yet on the Node.js side and send it over to the S3 bucket. An example request.file is the below:

{
    fieldname: 'cameraPhoto',
    originalname: 'picName',
    encoding: '7bit',
    mimetype: 'image/jpg',
    buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff e1 00 58 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 02 01 12 00 03 00 00 00 01 00 01 ... 1204620 more bytes>,
    size: 1204670
}

Any help is much appreciated!



Solution 1:[1]

For error message:

Multer: field value too long

with code:

app.post('/', upload.single('cameraPhoto'), async function (request, response, next) {
  // ...
  // ...
}

You can see issue 436 at Github Multer, that describe how set default limit size of upload files.

Best response at above issue purpose increase limit with usage of:

multer({
  limits: { fieldSize: 1_000_000 }
})

value is in bytes default value is 1MB

can see additional resource at express documentation, express middleware multer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GABORIEAU