繁体   English   中英

Node Js(Express) - POST req.body 为空(不寻常)

[英]Node Js(Express) - POST req.body is empty (unusual)

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

Angular组件

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

angular 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. angular 组件是从对其端点的外部调用激活的 (localhost:3000/dealer?code='1234'。代码在组件的构造函数中成功检索并分配给 authCode。 2. angular http 调用源自 ngOnInit。我试图在渲染组件页面之前获取一些信息并更新数据库。

我在用

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"

然而......我得到空的req.body。

顺便说一句....它适用于 postman

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

抱歉这么久……但我很困惑

urlencoded express 中间件仅在请求的Content-Typetype选项匹配时才解析正文。 默认情况下,类型选项是application/x-www-form-urlencoded 将请求的 Content-Type 从text/plain设置为application/x-www-form-urlencoded ,或将{"type": "text/plain"}传递给urlencoded(...)以覆盖默认行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM