常见错误:使用nodejs请求flask出现400错误

问题:使用nodejs请求flask出现400错误,如下图:

vscode:nodejs

pycharm:python-flask

nodejs代码

const https = require('https')

const option = {
    hostname: '127.0.0.1',
    port: 5000,
    path: '/manage/mall/pictures/name',
    method: 'GET',
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
    }
}

const req = https.request(option, res => {
  res.on('data', d => {
      process.stdout.write(d)
  })
})

req.on('error', error => {
  console.error(error)
})

req.end()

问题原因

flask没有添加SSL证书,所以将请求方式从https改为http即可解决问题

改正后的nodejs代码

const http = require('http')

const option = {
    hostname: '127.0.0.1',
    port: 5000,
    path: '/manage/mall/pictures/name',
    method: 'GET',
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
    }
}

const req = http.request(option, res => {
  res.on('data', d => {
      process.stdout.write(d)
  })
})

req.on('error', error => {
  console.error(error)
})

req.end()

 

改正后的输出结果:

猜你喜欢

转载自blog.csdn.net/Gragon_Shao/article/details/112637848