关键是使用"Content-Type": "text/plain"
@MadhuBhat 提到的。
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 http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName
来源:https : //stackoverflow.com/a/53501339/3850405