# 1. 使用koa-body中间件后ctx.request.body内容为空

## 出现场景

在使用koa-body 做文件上传的时候,发现使用DELETE请求时,request.body中的内容为空对象{}

### app.js

```javascript
//code...
const Koa = require('koa');

const koaBody = require("koa-body");

app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 200 * 1024 * 1024
    }
}))
//...code
```

## 问题原因

查阅[官方文档](https://github.com/dlau/koa-body)找到原因<br>

![文档图片](/files/-Lp1vYBkqcsDDFiN1QPM)

**strict {Boolean} DEPRECATED If enabled, don't parse GET, HEAD, DELETE requests, default true**

strict 参数:如果启用，则不解析GET，HEAD，DELETE请求，默认为true

## 解决方案

```javascript
//...code
app.use(koaBody({
    multipart: true,
    strict:false,//设为false
    formidable: {
        maxFileSize: 200 * 1024 * 1024
    }
}))
//...code
```

## 接下来即可正常解析body中的内容

```javascript
// 前端请求(jquery)
$.ajax({
    url:`${baseUrl}/xxx`,
    type:"DELETE",
    headers:{
        "content-type":"application/json"
    },
    data:{
        name:"小明",
        age:18
    }
}).then(res=>{
    console.log(res);
})


// 后端处理函数部分
const fn_testDelete=async(ctx,next)=>{
    const {name,age}=ctx.request.body;
    console.log(name,age);//小明 18
    ctx.response.body={
        code:200,
        errMsg:"OK"
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sugar-at.gitbook.io/blog-article/node/p1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
