首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法查看授权头

问无法查看授权头
EN

Stack Overflow用户
提问于 2020-08-03 09:11:41
回答 1查看 158关注 0票数 0

我使用的是node.js默认的HTTP模块,并且有一个HTTP 。

我使用request.headers获取所有标头,但是当我尝试执行request.headers.authorization时,它返回未定义的,但是有授权就像你在这里看到的。

我尝试执行JSON.parse(request.headers).authorization,但仍未定义,并使进程崩溃。如何获得授权头内容?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-03 09:23:32

也许你可以用request.getHeader(name)我在名字里看过的方法

编辑1:

index.js

代码语言:javascript
复制
const http = require('http');

const server = http.createServer((request, response) => {
  console.log(request.headers);
  console.log(request.headers.authorization);
  console.log('----');

  const headers = {
    'Content-Type': 'text/plain',
  };
  let statusCode = 404;
  response.writeHead(200, headers);
  response.end('Hi');
});

server.listen(8080, () => {
 console.log(`Server listening on port :8080 `);
});

在一号终端执行:

node index.js

第二航站楼:

curl localhost:8080 -H 'authorization: hello'

一号终端的输出是:

代码语言:javascript
复制
{ host: 'localhost:8080',
  'user-agent': 'curl/7.68.0',
  accept: '*/*',
  authorization: 'hello' }
hello
----
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63226674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档