Presigned POST File Submission Invalid JSON


Hello there,

I am trying to send data from my website to my S3 Bucket with pre-signed POST. I was pretty sure I created my form-data correctly but error returned was:

<?xml version="1.0" encoding="UTF-8"?>
<Message>Invalid Policy: Invalid JSON.</Message>

Here's my back-end code that generates the pre-signed POST's required form data:


  • Get Signed POST form data for uploading medias */ router.get('/postdata', function (req, res, next) { let postdata; let id = uuid(); s3.createPresignedPost({ Bucket: 'bucket', Conditions: [ { "bucket": "bucket" }, ["eq", "$key", 'file'] ["content-length-range", 1, 1024 * 1024 * 15], {"success_action_status": "200"} ], Expires: 300 }, (err, data) => { data.fields.key = 'file' = id; data.fields.success_action_status = "200"; postdata = data; });

    res.json(postdata); });

And here's how I generated the FormData object:

let form = new FormData(); Object.keys(result.fields).forEach(key => { form.append(key, result.fields[key]); });

form.append('file', document.getElementById('file').value);

The request body is also properly being encoded as multipart/form-data.   
So my question is, what part of this is "Invalid JSON". Also, what I don't understand even more is I have a working example but this one does just does not work anyway and anyhow I tried it.
asked 5 years ago359 views
2 Answers

OK so I just realized that I needed to include the Content-Type header with the request.

fetch(action, {
    method: "POST",
    headers: {
        "Content-Type": "...."
    body: form

But when I set my Content-Type to "multipart/form-data", I'm getting this error:

<?xml version="1.0" encoding="UTF-8"?>
<Message>The body of your POST request is not well-formed multipart/form-data.</Message>

And when I set it to undefined, null or false like what I've read from many sources, I get this:

<?xml version="1.0" encoding="UTF-8"?> <Error> <Code>PreconditionFailed</Code> <Message>At least one of the pre-conditions you specified did not hold</Message><Condition>Bucket POST must be of the enclosure-type multipart/form-data</Condition> <RequestId>E2993D140D291888</RequestId> <HostId>+HZFLQbfjAWBQ7SY8FXWh4hUarRr2YefjOENWH/Eh7XZzuxyfa5DMy1didMZWhPA14XW3ql47iQ=</HostId> </Error >```

Also if I did not specify the Content-Type, I will go back to receiving Invalid JSON error like before

EDIT: Actually, not setting Content-Type leaves my browser to set the correct Content-Type and its boundary. But I'm still getting Invalid JSON error...

answered 5 years ago

Ok, sorry. Silly me.

Conditions: [
    { "bucket": "bucket" },
    ["eq", "$key", 'file'] (<-------- MISSING COMMA)
    ["content-length-range", 1, 1024 * 1024 * 15],
    {"success_action_status": "200"}

Problem solved.

Edited by: ionizer on Nov 14, 2017 1:30 AM

answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions