1

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

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, actually, I have a URL, do I still need to createClient?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.