关键在于使用@MadhuBhat提到的
"Content-Type": "text/plain"
。
axios.post(path, code, { headers: { "Content-Type": "text/plain" } }).then(response => {
console.log(response);
});
如果你使用.NET
,需要注意的是一个直接传递字符串给控制器会返回415 Unsupported Media Type
报错。为了解决这个问题,你需要将字符串用连字符包裹起来,并以"Content-Type": "application/json"
的形式发送。
axios.post(path, "\"" + code + "\"", { headers: { "Content-Type": "application/json" } }).then(response => {
console.log(response);
});
C# 控制器:
[HttpPost]
public async Task<ActionResult<string>> Post([FromBody] string code)
{
return Ok(code);
}
https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers
如果有必要,您也可以使用查询参数进行POST请求:
.post(`/mails/users/sendVerificationMail`, null, { params: {
mail,
firstname
}})
.then(response => response.status)
.catch(err => console.warn(err));
这将使用两个查询参数POST一个空主体:
POST
http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName
来源:https://dev59.com/tlQJ5IYBdhLWcg3wl3Fa#53501339
json
还需要什么吗? - Timo