When I execute the following code from my browser the server gives me 400 and complains that the request body is missing. Anybody got a clue about how I can pass a simple st
this worked for me.
let content = 'Hello world';
static apicall(content) {
return axios({
url: `url`,
method: "put",
data: content
});
}
apicall()
.then((response) => {
console.log("success",response.data)
}
.error( () => console.log('error'));
simply put in headers 'Content-Type': 'application/json'
and the sent data in body JSON.stringify(string)
This worked for me:
export function modalSave(name,id){
console.log('modalChanges action ' + name+id);
return {
type: 'EDIT',
payload: new Promise((resolve, reject) => {
const value = {
Name: name,
ID: id,
}
axios({
method: 'put',
url: 'http://localhost:53203/api/values',
data: value,
config: { headers: {'Content-Type': 'multipart/form-data' }}
})
.then(function (response) {
if (response.status === 200) {
console.log("Update Success");
resolve();
}
})
.catch(function (response) {
console.log(response);
resolve();
});
})
};
}
I solved this by overriding the default Content-Type:
const config = { headers: {'Content-Type': 'application/json'} };
axios.put(url, content, config).then(response => {
...
});
Based on m experience, the default Conent-Type is application/x-www-form-urlencoded for strings, and application/json for objects (including arrays). Your server probably expects JSON.
axios.put(url,{body},{headers:{}})
example:
const body = {title: "what!"}
const api = {
apikey: "safhjsdflajksdfh",
Authorization: "Basic bwejdkfhasjk"
}
axios.put('https://api.xxx.net/xx', body, {headers: api})
This works for me (code called from node js repl):
const axios = require("axios");
axios
.put(
"http://localhost:4000/api/token",
"mytoken",
{headers: {"Content-Type": "text/plain"}}
)
.then(r => console.log(r.status))
.catch(e => console.log(e));
Logs: 200
And this is my request handler (I am using restify):
function handleToken(req, res) {
if(typeof req.body === "string" && req.body.length > 3) {
res.send(200);
} else {
res.send(400);
}
}
Content-Type header is important here.