I am trying to convert the Python request code into Nodejs, however, I cannot find the corresponding usage in Nodsjs. I want to POST request using authentication looks like this
Python code:
resp = requests.post(url, auth=HTTPBasicAuth(username, password), verify=False)
print resp.content
When I use the code in Nodejs:
request.post(url).auth('username', 'password', false);
It is not working. I have no idea how to pass the auth in the post function. Any helps
eyllanesc
246k19 gold badges205 silver badges282 bronze badges
1 Answer 1
You can try:
var client = http.createClient(80, 'www.site.tld');
var user = 'user';
var passwd = 'pa$$';
var auth = 'Basic ' + Buffer.from(user + ':' + passwd).toString('base64');
var header = {'Host': 'www.site.tld', 'Authorization': auth};
var request = client.request('POST', '/', header);
answered Apr 2, 2019 at 22:17
Pedro Lobito
99.8k36 gold badges274 silver badges278 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Akira
Thanks for the reply, actually, I have a URL, do I still need to createClient?
default