I'm building a Vue app with a Rails backend. I'm following some articles online that suggested a workflow in which I:
- Let my Rails API generate a pre-signed S3 url
- Which I get through an API request in my Vue app
- I use the data from that request to POST the actual image directly to S3
The first two steps are working fine, but I'm struggling to understand how to include the filedata in the request that is of type: 'multipart/form-data'.
My Code is as follows, I use vuetify as a component library:
<template>
<v-form>
<v-file-input v-model="file" label="File input"></v-file-input>
<v-btn class="mt-2" block bottom color="primary" @click="submit">Opslaan</v-btn>
</v-form>
</template>
<script>
import { axios_request } from '../_helpers';
export default {
name:'AccountImageForm',
data: () => ({
file: {}
}),
methods: {
filesChange(event) {
this.file = event.target.files[0]
},
submit() {
axios_request('/api/v1/images/upload_url')
.then(
response => {
// this response contains the pre-signed info
var data = {
...response.url_fields,
file: this.file
}
var headers = { 'Content-Type': 'multipart/form-data' }
axios_request(response.url, 'POST', data, headers)
.then(
response => {
console.log(response)
}
)
}
)
},
}
}
</script>
This requests fails with the following error
<Error>
<Code>MalformedPOSTRequest</Code>
<Message>The body of your POST request is not well-formed multipart/form-data.</Message>
<RequestId>x</RequestId>
<HostId>xx</HostId>
</Error>
When I look at my original formData, it seems that the filedata is empty. Also the size of the request is not big enough, so my assumption is that the file is missing. Is there some additional work to serialize the file for this request?
Thanks