I hava an application doing a CRUD, but when i try to use the method delete, i keep getting a 406 (Not Acceptable), I’m using an API formated in json_api, so i need personalized headers, in all other methods its working, but only delete not.
My axios.js
JavaScript
x
8
1
import axios from 'axios';
2
3
const api = axios.create({
4
baseURL: 'http://localhost:3000',
5
});
6
7
export default api;
8
My headers.js
JavaScript
1
31
31
1
import api from './axios';
2
3
const authHeader = {
4
headers: {
5
Accept: 'application/vnd.api+json',
6
'content-type': 'application/vnd.api+json',
7
},
8
};
9
10
const body = {
11
body: {
12
name: 'Vitor Queiroz',
13
},
14
};
15
16
async function getAuth() {
17
return api.post('/auths', body, authHeader).then((response) => {
18
return response.data.token;
19
});
20
}
21
22
const headers = {
23
headers: {
24
Accept: 'application/vnd.api+json',
25
'content-type': 'application/vnd.api+json',
26
authorization: `Bearer ${await getAuth()}`,
27
},
28
};
29
30
export default headers;
31
And my editUnit.vue
JavaScript
1
92
92
1
<template>
2
<form action="">
3
<label for="name">Nome da Unidade de Medida</label>
4
<input type="text" v-model="name" />
5
<br />
6
<button type="submit" @click.prevent="editUnit(id)">Editar</button>
7
<button type="submit" @click.prevent="deleteUnit(id)">Excluir</button>
8
<div v-if="status === '200'">
9
<p>Unidade de Medida Atualizada com Sucesso</p>
10
</div>
11
<div v-else-if="status === '204'">
12
<p>Unidade de Medida Excluida com sucesso</p>
13
</div>
14
<div v-else-if="status !== '201' && status !== '' && status !== '204'">
15
<p>Ocorreu um erro {{ this.error }} inesperado, tente novamente.</p>
16
</div>
17
</form>
18
</template>
19
20
<script lang="ts">
21
import { defineComponent } from 'vue';
22
import api from '../../services/axios.js';
23
import headers from '../../services/headers.js';
24
25
export default defineComponent({
26
name: 'createUnits',
27
props: {
28
id: {
29
type: String,
30
},
31
},
32
data() {
33
return {
34
myList: '',
35
name: '',
36
status: '',
37
error: '',
38
aaa: this.id,
39
};
40
},
41
beforeMount() {
42
api.get(`/units/${this.aaa}`, headers).then((res) => {
43
this.myList = res.data.data;
44
this.name = this.myList.attributes.name;
45
});
46
},
47
methods: {
48
editUnit(id) {
49
const body = {
50
id: `${id}`,
51
name: `${this.name}`,
52
};
53
api
54
.patch(`/units/${id}`, body, headers)
55
.then((res) => {
56
if (res.status == 200) {
57
this.status = '200';
58
}
59
})
60
.catch(
61
(err) => (
62
(this.error = err.response.status),
63
(this.status = err.response.status)
64
)
65
);
66
},
67
68
deleteUnit(id) {
69
const body = {
70
id: `${id}`,
71
};
72
api
73
.delete(`/units/${id}`, { data: { headers } })
74
.then((res) => {
75
console.log(id);
76
if (res.status == 204) {
77
this.status = '204';
78
}
79
})
80
.catch(
81
(err) => (
82
(this.error = err.response.status),
83
(this.status = err.response.status)
84
)
85
);
86
},
87
},
88
});
89
</script>
90
91
<style scoped></style>
92
The code referent to the delete method is between line 68-86
And my error in the api terminal is:
JavaScript
1
6
1
Started DELETE "/units/7" for 127.0.0.1 at 2022-08-16 15:02:33 -0300
2
Processing by UnitsController#destroy as HTML
3
Parameters: {"id"=>"7"}
4
Filter chain halted as :ensure_json_request rendered or redirected
5
Completed 406 Not Acceptable in 0ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 87)
6
Advertisement
Answer
The delete function is not correct
JavaScript
1
20
20
1
deleteUnit(id) {
2
const body = {
3
id: `${id}`,
4
};
5
api
6
.delete(`/units/${id}`, { headers })
7
.then((res) => {
8
console.log(id);
9
if (res.status == 204) {
10
this.status = '204';
11
}
12
})
13
.catch(
14
(err) => (
15
(this.error = err.response.status),
16
(this.status = err.response.status)
17
)
18
);
19
}
20