question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: The body of your POST request is not well-formed multipart/form-data.

See original GitHub issue

I am using angular file upload and getting the error message: The body of your POST request is not well-formed multipart/form-data.

The purpose is to upload a file to S3 using this method.
http://aws.amazon.com/articles/1434

Here is the code:

$scope.onFileSelect = function($files) {

            for (var i = 0; i < $files.length; i++) {
                var $file = $files[i];

                AWS.get({partId: $stateParams.id, bucket: ""}, function(response){

                    var formData = {
                        key: "/OEM/PARTS/" + $stateParams.id + "/" + $file.name,
                        AWSAccessKeyId: "AKIAJDWFD4T7GQZNZBNQ",
                        acl: 'private',
                        success_action_redirect: "#",
                        policy: response.policy,
                        signature: response.signature,
                        'Content-Type': $file.type
                    };

                    $http.uploadFile({
                        method: 'POST',
                        url: 'https://tennex.s3.amazonaws.com/',
                        headers: {'Content-Type': 'multipart/form-data',
                            //this clears the default Authorization code.
                            Authorization: ""},
                        data: formData,
                        file: $file
                    }).progress(function(evt) {
                        console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
                    }).then(function(data, status, headers, config) {
                        // file is uploaded successfully
                        console.log(data);
                    });
                }, function(){
                    alert("failed to download policy file");
                });
            }
        };

Here is the request and response http headers:

Request URL:https://tennex.s3.amazonaws.com/ Request Method:POST Status Code:400 Bad Request Request Headersview source Accept:/ Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Authorization: Cache-Control:no-cache Connection:keep-alive Content-Length:319662 Content-Type:multipart/form-data Host:tennex.s3.amazonaws.com Origin:http://localhost:63342 Pragma:no-cache Referer:http://localhost:63342/app/views/OEM/index.html User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 Request Payload ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“key”

/OEM/PARTS/30/Tesla 2013 05 17.pdf ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“AWSAccessKeyId”

AKIAJDWFD4T7GQZNZBNQ ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“acl”

private ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“success_action_redirect”

------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“policy”

eydleHBpcmF0aW9uJzogJzIwMTMtMTAtMjJUMjI6NTM6NTZaJywgJ2NvbmRpdGlvbnMnOiBbIHsnYnVja2V0JzogJ3Rlbm5leCd9LCBbJ3N0YXJ0cy13aXRoJywgJyRrZXknLCAnT0VNL1BBUlRTLzMwLycgXSwgeydhY2wnOiAncHJpdmF0ZSd9LCB7J3N1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0JzogJyMnLCB9IFsnc3RhcnRzLXdpdGgnLCAnJENvbnRlbnQtVHlwZScsICcnXV19IA== ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“signature”

a55375e793cf0debb8f1077c760b3aa00cff43dd ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“Content-Type”

application/pdf ------WebKitFormBoundary85lPiaZFfRGERy4T Content-Disposition: form-data; name=“file”; filename=“Tesla 2013 05 17.pdf” Content-Type: application/pdf

------WebKitFormBoundary85lPiaZFfRGERy4T– Response Headersview source Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:GET, PUT, POST Access-Control-Allow-Origin:http://localhost:63342 Access-Control-Max-Age:3000 Connection:close Content-Type:application/xml Date:Tue, 22 Oct 2013 22:44:05 GMT Server:AmazonS3 Transfer-Encoding:chunked Vary:Origin, Access-Control-Request-Headers, Access-Control-Request-Method x-amz-id-2:wkp3NyL+kJBebuNUN9RYEvsDYLGZEKHXm7rqB7VuxVmPDEAFnXX0SZuo8Dy88hBG x-amz-request-id:80A54AF54999B9EB

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:50 (21 by maintainers)

github_iconTop GitHub Comments

34reactions
danialfaridcommented, Oct 22, 2013

Remove ‘Content-Type’: ‘multipart/form-data’, from headers and see if it works. Otherwise send an upload request using regular form with action like the amazon template and catch the content of the requests. Then we can compare what is the difference and what is missing in the data.

10reactions
JettBurns14commented, Mar 3, 2021

Wow 8 year old thread, but still want to leave my solution for folks who run across this thread like I did. I was getting the same error from AWS S3:

<Error><Code>MalformedPOSTRequest</Code><Message>The body of your POST request is not well-formed multipart/form-data.</Message></Error>

Ended up fixing this by adding 5000 to the Content-Length header, which had already been set to the video size in bytes I was uploading to AWS S3 with NodeJS Axios. I assume my specified Content-Length value was shorter than the actual request length, and AWS didn’t like that. Very useful error they spit at me… Also, note that I used ...fd.getHeaders(), this is important to keep for setting other headers.

Here’s the final working code I used:

const FormData = require("form-data");
const fs = require("fs");
const axios = require("axios");

async function uploadVid(text, path, file_name, aws) {
    let vidLength = fs.statSync(path).size;
    let fd = new FormData();
    fd.append("key", aws.key);
    fd.append("AWSAccessKeyId", aws.awsAccessKeyId);
    fd.append("acl", aws.acl);
    fd.append("success_action_status", aws.successActionStatus);
    fd.append("x-amz-security-token", aws.sessionToken);
    fd.append("policy", aws.policy);
    fd.append("signature", aws.signature);
    fd.append("file", fs.createReadStream(path), file_name);

    let res = await axios({
        url: "https://" + aws.instanceBucketName + ".s3.amazonaws.com/",
        method: "POST",
        data: fd,
        headers: {
	    "Content-Length": vidLength + 5000,
            ...fd.getHeaders()
        }
    });
    return res;
};
Node v14.16.0
npm v7.6.0
Axios v0.21.1
form-data v3.0.1

Very frustrating to debug, and never found any help through AWS docs. I sent them feedback to improve the docs or the API error message. Hope this is helpful to someone down the road!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios multipart/form-data request return as "request is not well ...
The new FormData(...) function does not correspond with the Content-Type "multipart/form-data". I suggest converting it to JSON data and then ...
Read more >
error while upload file using http client? - MSDN - Microsoft
I get an error that the The body of your POST request is not well-formed multipart/form-data. If I clear the multiform data's header...
Read more >
Presigned POST File Submission Invalid JSON - AWS re:Post
I was pretty sure I created my form-data correctly but error returned was: ``` ... body of your POST request is not well-formed...
Read more >
Error 400 on API request? - Pipedrive Developers' Community
Error Code = MalformedPOSTRequest. Message = The body of your POST request is not well-formed multipart/form-data.
Read more >
Question: How to upload a file using HTTP POST Form-Data?
To answer your question. Yes it should be possible to create this request. However the script whould need to be modified a bit....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found