Dam*_*lot 5 api rest json insomnia deno
我目前正在使用Deno开发一个概念验证 REST api 应用程序,我的post 方法(getAll et getworking)有问题。我的请求正文不包含随 Insomnia 发送的数据。
我的方法:
addQuote: async ({ request, response }: { request: any; response: any }) => {
const body = await request.body();
if (!request.hasBody) {
response.status = 400;
response.body = { message: "No data provided" };
return;
}
let newQuote: Quote = {
id: v4.generate(),
philosophy: body.value.philosophy,
author: body.value.author,
quote: body.value.quote,
};
quotes.push(newQuote);
response.body = newQuote;
},
Run Code Online (Sandbox Code Playgroud)
要求 :
回复 :
我把它放在Content-Type - application/json
标题中。
如果我只返回body.value
,它是空的。
感谢帮助 !
由于值类型是承诺,我们必须在访问值之前解析。
尝试这个:
addQuote: async ({ request, response }: { request: any; response: any }) => {
const body = await request.body(); //Returns { type: "json", value: Promise { <pending> } }
if (!request.hasBody) {
response.status = 400;
response.body = { message: "No data provided" };
return;
}
const values = await body.value;
let newQuote: Quote = {
id: v4.generate(),
philosophy: values.philosophy,
author: values.author,
quote: values.quote,
};
quotes.push(newQuote);
response.body = newQuote;
}
Run Code Online (Sandbox Code Playgroud)