Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rawResponse能否给一个method为get的例子? #136

Open
xiebruce opened this issue May 5, 2024 · 1 comment
Open

rawResponse能否给一个method为get的例子? #136

xiebruce opened this issue May 5, 2024 · 1 comment

Comments

@xiebruce
Copy link

xiebruce commented May 5, 2024

rawResponse能否给一个method为get的例子?我需要从req中获取get请求传过来的参数,http:127.0.0.1:5173/mock/admin/login?a=1&b=2,我要获取到这个a和b的值,现有的例子是post的

{
    url: '/api/text',
    method: 'post',
    rawResponse: async (req, res) => {
      let reqbody = ''
      await new Promise((resolve) => {
        req.on('data', (chunk) => {
          reqbody += chunk
        })
        req.on('end', () => resolve(undefined))
      })
      res.setHeader('Content-Type', 'text/plain')
      res.statusCode = 200
      res.end(`hello, ${reqbody}`)
    },
  },
@XiaopingHu
Copy link

这个看源码就知道怎么处理了,导入一个url模块,用url.parse(req.url, true)就能获得
或者使用response选项,在方法中,用this.req或this.res就能获取到req和res,this.parseJson()就能获取到body参数

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants