Deno post 方法空请求 body

Deno post method empty request body

我目前正在使用 Deno 开发 proof-of-concept REST api 应用程序,我的 post 方法(getAll et 开始工作)。我请求的 body 不包含随 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;
  },

要求:

响应:

我把Content-Type - application/json放在header里了。
如果我return只有body.value,它是空的。

感谢帮助!

由于值类型是 promise 我们必须在访问值之前解析。

试试这个:

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;
  }