Bad request status 400 when uploading a file
<template>
<div>
<input type="file" id="file" ref="file" />
<button @click="submit()">Upload File</button>
</div>
</template>
<script>
import qs from "querystring"
import axios from "axios"
//axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "box_visitor_id"
export default {
data() {
return {
attributes:"",
file: "",
//config: configdata
access_token:""
}
},
methods: {
submit(){
const requestBody = {
client_id: '************',
client_secret: **************,
grant_type: 'client_credentials',
box_subject_type: 'enterprise',
box_subject_id: ***************
//Just hid and they are working fine....
}
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
const url = "https://api.box.com/oauth2/token/"
axios.post(url, qs.stringify(requestBody), config)
.then((result) => {
console.log(result);
let a = result.data.access_token
console.log(a);
this.file = this.$refs.file.files[0];
console.log(this.file);
var attributes = { name:"File name",
parent : {
id : "0"
},
content_created_at :"" ,
content_modified_at: ""
}
console.log(attributes);
let stringify_attrtibutes = JSON.stringify(attributes)
console.log(stringify_attrtibutes);
let formData = new FormData();
formData.append("attributes",stringify_attrtibutes)
formData.append("file",this.file)
console.log("formData")
for (var value of formData.values()) {
console.log(value);
}
const config2 = {
headers: {
//'content-md5' : '025C42C7B1FB2A7CE5709B4D60A5420444866372',
'Authorization' : 'Bearer' + a,
'Content-Type' : 'multipart/form-data'
}
}
const url2 = "https://upload.box.com/api/2.0/files/content/";
//console.log(requestBody2);
// console.log(url2);
// console.log(config2);
axios.post(url2,formData,config2)
.then((result) => {
console.log(result);
})
.catch((err) => {
console.log(err);
// Do somthing
})
})
.catch((err) => {
console.log(err);
// Do somthing
})
console.log(this.access_token);
},
}
}
</script>
<style lang="scss">
</style>
サインインしてコメントを残してください。
コメント
1件のコメント