Node Js(Express)-POST请求正文为空(异常)

时间:2020-04-14 01:21:07

标签: node.js angular express

我没有意识到这个问题有多么普遍和棘手。我花了很多时间来回顾所有以前的情况和答案。不用说,没有一个适用。 我正在从Angular 5向nodejs / express url进行httpClient POST调用。该应用程序进行了许多这样的调用,并且除了该调用以外的所有调用都可以进行:

角度组件

 this.ezzy.post(this.config.api.createConnectAccount, this.AuthCode, true, true, true)
                     .subscribe((data) => {
              if (data.code === '0') {

有角度的http通话

 ngOnInit() {
   ........
  createConnectAccount(url, body, loadingIcon, withCredentials, showErrorToast) {
       console.log(`CREATE CONNECT ACCOUNT....${url}...${JSON.stringify(body)}`);
      const headers = this.ezzy.preAjax(loadingIcon, withCredentials);
      return this.http.post(url, body, { withCredentials, headers })
        .map((res) => this.ezzy.postAjax(res, showErrorToast))
        .catch((err) => this.ezzy.handleError(err));

     }

我可以确认url和authCode / body都是正确的,并在这一点上提出。

router.post(Nodejs)

router.post('/users/createConnectAccount', async(req, res, next) => {

   // console.log (`REQ BODY FROM PAYOUT DASH:  ${JSON.stringify(req)}`);
    console.log(`ENTER CREATE CONNECT ACCOUNT...code......${req.body.code}`);
    console.log(`ENTER CREATE CONNECT ACCOUNT..body......${JSON.stringify(req.body)}`);
    console.log(`REQ HEADERS: ${JSON.stringify(req.headers)}`);

以下是与其他类似电话的区别: 1.从外部调用到其端点(本地主机:3000 / dealer?code ='1234')激活了角度组件,该代码已在组件的构造函数中成功检索并分配给authCode。 2.在ngOnInit内部产生的有角度的http调用。我正在尝试获取一些信息并在呈现组件页面之前更新数据库。

我正在使用

app.use(json());
app.use(urlencoded({
  extended: true
}));

和调用之前的req.header的console.log:

 ENTER CREATE CONNECT ACCOUNT...code......undefined
ENTER CREATE CONNECT ACCOUNT..body......{}
REQ HEADERS: {"host":"localhost:3000","connection":"keep-alive","content-length":"35","accept":"application/json, 
text/plain, */*","sec-fetch-dest":"empty","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36","content-type":"text/plain","origin":"http://localhost:3000","sec-fetch-site":"same-origin","sec-fetch-mode":"cors","referer":"http://localhost:3000/payout-dashboard?code=ac_H5nP4MUbEbp94K13jkA5h1DRG6f6pgOn&state=2lt8v9le8a5","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9","cookie":"connect.sid=s%3AsWLHYTY02P2EvYZy1FIVQzZLC6M0vR5p.GnU%2BU20RcjPYeG3lAUEDV9q1vmLceBPAfEE488ej5M4; _ga=GA1.1.695338957.1586021131; _gid=GA1.1.1793736642.1586291730; PDToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6InNlYWthaG1haWxAZ21haWwuY29tIiwibmlja25hbWUiOiIiLCJjYXRlZ29yeSI6IiIsImlhdCI6MTU4NjgyMDYyMSwiZXhwIjoxNjE4MzU2NjIxfQ.09gx1F_YJPxAs7BiiYToetdJhjd5DsUUkdoo3leFscU; io=yysQe40_plBblVuSAAAA"} 

如果您发现内容类型为:

 "content-type":"text/plain"

可接受的是:

"accept":"application/json, 
    text/plain, */*"

并且代码存在:

code=ac_H5nP4MUbEbp94K13jkA5h1DRG6f6pgOn&state=2lt8v9le8a5"

还...我得到的是空的请求单。

顺便说一句...。它是邮递员的作品

ENTER CREATE CONNECT ACCOUNT...code......ac_H5ikfuYleQromTeP5LnHGEmfEWaYD3he
ENTER CREATE CONNECT ACCOUNT..body......{"code":"ac_H5ikfuYleQromTeP5LnHGEmfEWaYD3he"}
REQ HEADERS: {"user-agent":"PostmanRuntime/7.24.1",
"accept":"*/*","postman-token":"0d5faea6-4684-408e-9235-c5e14b306918",
"host":"localhost:3000",

"accept-encoding":"gzip, 
deflate, br","connection":"keep-alive",
"content-type":"application/x-www-form-urlencoded",
"content-length":"40","cookie":"connect.sid=s%3ASahJY3VqXVjTjXF1X-SlU_9Shexa59Tm.Q0SRM1h%2FxJnoEnjS3u3I3x%2F%2FnLs%2FLzyiHGoJNuo0U7M"}

抱歉这么长...但是我很困惑

1 个答案:

答案 0 :(得分:0)

urlencoded表示中间件仅在the Content-Type of the request matches the type option.时解析正文,默认情况下,类型选项为application/x-www-form-urlencoded。将请求的内容类型从text/plain设置为application/x-www-form-urlencoded,或者将{"type": "text/plain"}传递给urlencoded(...),以覆盖默认行为。