简体   繁体   English

带有用户名的Node.js HTTPS请求

[英]Node.js HTTPS request with username

I'm trying to query api.whatever.com, but I also want to pass a USERNAME, such as in curl it would look like this: 我正在尝试查询api.whatever.com,但我也想传递一个USERNAME,例如在curl中,它看起来像这样:

https://USERNAME@api.whatever.com/ HTTPS://USERNAME@api.whatever.com/

How do I write the client request in such a way that USERNAME gets sent properly to the API server. 如何以USERNAME正确发送到API服务器的方式编写客户端请求。

http://en.wikipedia.org/wiki/Basic_access_authentication http://en.wikipedia.org/wiki/Basic_access_authentication

There's an explaination of the format you have to use as well as an example. 有一个你必须使用的格式的解释以及一个例子。

You need to base64 encode the username. 你需要base64编码用户名。

url = "https://USERNAME@api.whatever.com/".replace("https://","").split("@");
// Buffer is global, no need for require.
url64 = "https://" + (new Buffer(url[0]).toString('base64')) + url[1];

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

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