Reputation: 43
I'm working in Javascript (frontend) and have a colleague working in the backend with NodeJS. When calling a GET request, he asks me to put the data in the body, but I could not figure out how to do that. (If I use this code to a POST request, it works fine). Could you tell me if this is possible and how to do it? He says that it is possible, but I've googled a lot and could not find the correct way to do that. ERROR that I get: "Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body."
let URL = "http://localhost:3000/verifyUser";
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NjJDMTRBNk";
fetch(URL, {
method: request,
mode: 'cors',
body: JSON.stringify({
user: 'Carlos6',
password: '543534543',
email: "[email protected]"
}),
headers: {
'Accept' : 'application/json',
'Content-type': 'application/json; charset=UTF-8',
'auth-token': token
}
}).then(function (response) {
if (response.ok) {
return response.json();
}
return Promise.reject(response);
}).then(function (data) {
console.log(data);
}).catch(function (error) {
console.warn('Something went wrong.', error);
});
Upvotes: 0
Views: 2348
Reputation: 3520
You are using HTTP GET and sending a body.
If you want to send a body (JSON) you should use the PUT and POST.
The best will probably be to:
method: "PUT"
If you want to know which one to chose look at this question: ( PUT vs. POST in REST)
Upvotes: 4
Reputation: 22803
If you wish to send a request with a body then you should make a POST-request and not a GET one. GET-request cannot have a body by its nature and primary goal. All params of GET-request must be indicated in the URL itself only.
Upvotes: 1