ExpressJS: Send Request Header Authentication











up vote
0
down vote

favorite












I have an API to authenticate the user with LDAP and I have been provided with the information which includes Content-Type: application/json and username and password for the request header and then the user's username and password to be passed in body. I tried the below code but it's not working. I want to know if I am passing the header request correctly or not.



router.post('/user', function(req, res){
var auth = {
content-type: "application/json",
username: "abcd",
password: "xyze"
}

auth.post('/user', {username: req.body.username, password: req.body.password"}, function(response) {
console.log(response);
})

})









share|improve this question




















  • 1




    You are calling auth.post while there is no method like that in your object auth. What do you want to do?
    – tbking
    Nov 12 at 18:40










  • So I wanted to authenticate a user based on his username and password along with the header request. Can you tell me how can I do this on express?
    – Monsters
    Nov 13 at 4:20















up vote
0
down vote

favorite












I have an API to authenticate the user with LDAP and I have been provided with the information which includes Content-Type: application/json and username and password for the request header and then the user's username and password to be passed in body. I tried the below code but it's not working. I want to know if I am passing the header request correctly or not.



router.post('/user', function(req, res){
var auth = {
content-type: "application/json",
username: "abcd",
password: "xyze"
}

auth.post('/user', {username: req.body.username, password: req.body.password"}, function(response) {
console.log(response);
})

})









share|improve this question




















  • 1




    You are calling auth.post while there is no method like that in your object auth. What do you want to do?
    – tbking
    Nov 12 at 18:40










  • So I wanted to authenticate a user based on his username and password along with the header request. Can you tell me how can I do this on express?
    – Monsters
    Nov 13 at 4:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an API to authenticate the user with LDAP and I have been provided with the information which includes Content-Type: application/json and username and password for the request header and then the user's username and password to be passed in body. I tried the below code but it's not working. I want to know if I am passing the header request correctly or not.



router.post('/user', function(req, res){
var auth = {
content-type: "application/json",
username: "abcd",
password: "xyze"
}

auth.post('/user', {username: req.body.username, password: req.body.password"}, function(response) {
console.log(response);
})

})









share|improve this question















I have an API to authenticate the user with LDAP and I have been provided with the information which includes Content-Type: application/json and username and password for the request header and then the user's username and password to be passed in body. I tried the below code but it's not working. I want to know if I am passing the header request correctly or not.



router.post('/user', function(req, res){
var auth = {
content-type: "application/json",
username: "abcd",
password: "xyze"
}

auth.post('/user', {username: req.body.username, password: req.body.password"}, function(response) {
console.log(response);
})

})






node.js rest express http-headers http-post






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 18:37

























asked Nov 12 at 18:34









Monsters

11




11








  • 1




    You are calling auth.post while there is no method like that in your object auth. What do you want to do?
    – tbking
    Nov 12 at 18:40










  • So I wanted to authenticate a user based on his username and password along with the header request. Can you tell me how can I do this on express?
    – Monsters
    Nov 13 at 4:20














  • 1




    You are calling auth.post while there is no method like that in your object auth. What do you want to do?
    – tbking
    Nov 12 at 18:40










  • So I wanted to authenticate a user based on his username and password along with the header request. Can you tell me how can I do this on express?
    – Monsters
    Nov 13 at 4:20








1




1




You are calling auth.post while there is no method like that in your object auth. What do you want to do?
– tbking
Nov 12 at 18:40




You are calling auth.post while there is no method like that in your object auth. What do you want to do?
– tbking
Nov 12 at 18:40












So I wanted to authenticate a user based on his username and password along with the header request. Can you tell me how can I do this on express?
– Monsters
Nov 13 at 4:20




So I wanted to authenticate a user based on his username and password along with the header request. Can you tell me how can I do this on express?
– Monsters
Nov 13 at 4:20












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Consider use axios package to make your requests: https://www.npmjs.com/package/axios



This link has a section “Creating an instance showing how you can set the header:



var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});


Once the header is set, invoke the post method, like this:



instance.post('/my/specific/endpoint/', myData);


In the following example, you can pass an authentication token as the header to your request:



import axios from 'axios';
const MY_BASE_URL = 'https://www.myserver.com/';
class MyLdapService {
static xhr(accessToken) {
const options = {
baseURL: MY_BASE_URL,
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
};
return axios.create(options);
}
static async myPostEndpoint(accessToken, data) {
return new Promise((resolve, reject) => {
this.xhr(accessToken).post('my-endpoint/', data)
.then(result => resolve(result.data.card))
.catch(err => reject(this.createError(err)));
});
}
}


So you can invoke the POST endpoint like this:



MyLdapService.myPostEndpoint('my_access_token', {});





share|improve this answer























  • The code that you have written is for header authentication. Am I right?
    – Monsters
    Nov 13 at 4:22










  • Then how do I pass the instance to the post request?
    – Monsters
    Nov 13 at 4:22










  • Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
    – Vinicius Ronconi
    Nov 13 at 16:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268114%2fexpressjs-send-request-header-authentication%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Consider use axios package to make your requests: https://www.npmjs.com/package/axios



This link has a section “Creating an instance showing how you can set the header:



var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});


Once the header is set, invoke the post method, like this:



instance.post('/my/specific/endpoint/', myData);


In the following example, you can pass an authentication token as the header to your request:



import axios from 'axios';
const MY_BASE_URL = 'https://www.myserver.com/';
class MyLdapService {
static xhr(accessToken) {
const options = {
baseURL: MY_BASE_URL,
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
};
return axios.create(options);
}
static async myPostEndpoint(accessToken, data) {
return new Promise((resolve, reject) => {
this.xhr(accessToken).post('my-endpoint/', data)
.then(result => resolve(result.data.card))
.catch(err => reject(this.createError(err)));
});
}
}


So you can invoke the POST endpoint like this:



MyLdapService.myPostEndpoint('my_access_token', {});





share|improve this answer























  • The code that you have written is for header authentication. Am I right?
    – Monsters
    Nov 13 at 4:22










  • Then how do I pass the instance to the post request?
    – Monsters
    Nov 13 at 4:22










  • Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
    – Vinicius Ronconi
    Nov 13 at 16:19















up vote
0
down vote













Consider use axios package to make your requests: https://www.npmjs.com/package/axios



This link has a section “Creating an instance showing how you can set the header:



var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});


Once the header is set, invoke the post method, like this:



instance.post('/my/specific/endpoint/', myData);


In the following example, you can pass an authentication token as the header to your request:



import axios from 'axios';
const MY_BASE_URL = 'https://www.myserver.com/';
class MyLdapService {
static xhr(accessToken) {
const options = {
baseURL: MY_BASE_URL,
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
};
return axios.create(options);
}
static async myPostEndpoint(accessToken, data) {
return new Promise((resolve, reject) => {
this.xhr(accessToken).post('my-endpoint/', data)
.then(result => resolve(result.data.card))
.catch(err => reject(this.createError(err)));
});
}
}


So you can invoke the POST endpoint like this:



MyLdapService.myPostEndpoint('my_access_token', {});





share|improve this answer























  • The code that you have written is for header authentication. Am I right?
    – Monsters
    Nov 13 at 4:22










  • Then how do I pass the instance to the post request?
    – Monsters
    Nov 13 at 4:22










  • Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
    – Vinicius Ronconi
    Nov 13 at 16:19













up vote
0
down vote










up vote
0
down vote









Consider use axios package to make your requests: https://www.npmjs.com/package/axios



This link has a section “Creating an instance showing how you can set the header:



var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});


Once the header is set, invoke the post method, like this:



instance.post('/my/specific/endpoint/', myData);


In the following example, you can pass an authentication token as the header to your request:



import axios from 'axios';
const MY_BASE_URL = 'https://www.myserver.com/';
class MyLdapService {
static xhr(accessToken) {
const options = {
baseURL: MY_BASE_URL,
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
};
return axios.create(options);
}
static async myPostEndpoint(accessToken, data) {
return new Promise((resolve, reject) => {
this.xhr(accessToken).post('my-endpoint/', data)
.then(result => resolve(result.data.card))
.catch(err => reject(this.createError(err)));
});
}
}


So you can invoke the POST endpoint like this:



MyLdapService.myPostEndpoint('my_access_token', {});





share|improve this answer














Consider use axios package to make your requests: https://www.npmjs.com/package/axios



This link has a section “Creating an instance showing how you can set the header:



var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});


Once the header is set, invoke the post method, like this:



instance.post('/my/specific/endpoint/', myData);


In the following example, you can pass an authentication token as the header to your request:



import axios from 'axios';
const MY_BASE_URL = 'https://www.myserver.com/';
class MyLdapService {
static xhr(accessToken) {
const options = {
baseURL: MY_BASE_URL,
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
};
return axios.create(options);
}
static async myPostEndpoint(accessToken, data) {
return new Promise((resolve, reject) => {
this.xhr(accessToken).post('my-endpoint/', data)
.then(result => resolve(result.data.card))
.catch(err => reject(this.createError(err)));
});
}
}


So you can invoke the POST endpoint like this:



MyLdapService.myPostEndpoint('my_access_token', {});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 16:18

























answered Nov 12 at 22:28









Vinicius Ronconi

918




918












  • The code that you have written is for header authentication. Am I right?
    – Monsters
    Nov 13 at 4:22










  • Then how do I pass the instance to the post request?
    – Monsters
    Nov 13 at 4:22










  • Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
    – Vinicius Ronconi
    Nov 13 at 16:19


















  • The code that you have written is for header authentication. Am I right?
    – Monsters
    Nov 13 at 4:22










  • Then how do I pass the instance to the post request?
    – Monsters
    Nov 13 at 4:22










  • Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
    – Vinicius Ronconi
    Nov 13 at 16:19
















The code that you have written is for header authentication. Am I right?
– Monsters
Nov 13 at 4:22




The code that you have written is for header authentication. Am I right?
– Monsters
Nov 13 at 4:22












Then how do I pass the instance to the post request?
– Monsters
Nov 13 at 4:22




Then how do I pass the instance to the post request?
– Monsters
Nov 13 at 4:22












Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
– Vinicius Ronconi
Nov 13 at 16:19




Hey @Monsters, I edited my comment to give you more details on it. Hope it helps!
– Vinicius Ronconi
Nov 13 at 16:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268114%2fexpressjs-send-request-header-authentication%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)