HTTP request in node.js returning response.statuscode as forbidden





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I want to POST data in a 3rd part website through node.js but I am getting the error 403.
I have this code in browser :



$.ajax("/login",{ /// here i am in https://255.255.255 /// I am obscuring the real host ip

data:{
username:"username",
password: "password",
autologin:"true"},

method:"POST"

}).done(function(msg) {
console.log( msg );
/// another $.ajax request here
});


It works fine. The problem starts when I try to use it in node.js, I got this code after some help of SO users :



var request = require('request');

request({
method: "POST",
baseUrl: "https://255.255.255",
uri: "/login",
form: {
username: "username",
password: "password",
autologin: "true"
}
},
function (error, httpResponse, body) {
if (error) {
console.error(error);
}
console.log(httpResponse.statusCode);
console.log(body);
});


Here I get httpResponse.statusCode as 403 - forbidden.
I was not expecting it because my $.ajax code works and does not return me this error.



What I've tried so far :




  • set user-agent as "node.js", "request", "Mozilla/5.0"

  • use axios ( also got the error 403 )

  • use request-promise


also I would like to know :




  • how to see both codes running to know whats the difference between each other

  • more user-agents, and which one I've to use to simulate a browser

  • if there is a better npm package to post data in 3rd part websites


Thanks in advance.










share|improve this question























  • Maybe take a look at the response body of the failing HTTP request. It's possible that there's an error message.

    – Evert
    Jan 3 at 15:15











  • There is no error

    – HRP
    Jan 3 at 15:54


















-1















I want to POST data in a 3rd part website through node.js but I am getting the error 403.
I have this code in browser :



$.ajax("/login",{ /// here i am in https://255.255.255 /// I am obscuring the real host ip

data:{
username:"username",
password: "password",
autologin:"true"},

method:"POST"

}).done(function(msg) {
console.log( msg );
/// another $.ajax request here
});


It works fine. The problem starts when I try to use it in node.js, I got this code after some help of SO users :



var request = require('request');

request({
method: "POST",
baseUrl: "https://255.255.255",
uri: "/login",
form: {
username: "username",
password: "password",
autologin: "true"
}
},
function (error, httpResponse, body) {
if (error) {
console.error(error);
}
console.log(httpResponse.statusCode);
console.log(body);
});


Here I get httpResponse.statusCode as 403 - forbidden.
I was not expecting it because my $.ajax code works and does not return me this error.



What I've tried so far :




  • set user-agent as "node.js", "request", "Mozilla/5.0"

  • use axios ( also got the error 403 )

  • use request-promise


also I would like to know :




  • how to see both codes running to know whats the difference between each other

  • more user-agents, and which one I've to use to simulate a browser

  • if there is a better npm package to post data in 3rd part websites


Thanks in advance.










share|improve this question























  • Maybe take a look at the response body of the failing HTTP request. It's possible that there's an error message.

    – Evert
    Jan 3 at 15:15











  • There is no error

    – HRP
    Jan 3 at 15:54














-1












-1








-1








I want to POST data in a 3rd part website through node.js but I am getting the error 403.
I have this code in browser :



$.ajax("/login",{ /// here i am in https://255.255.255 /// I am obscuring the real host ip

data:{
username:"username",
password: "password",
autologin:"true"},

method:"POST"

}).done(function(msg) {
console.log( msg );
/// another $.ajax request here
});


It works fine. The problem starts when I try to use it in node.js, I got this code after some help of SO users :



var request = require('request');

request({
method: "POST",
baseUrl: "https://255.255.255",
uri: "/login",
form: {
username: "username",
password: "password",
autologin: "true"
}
},
function (error, httpResponse, body) {
if (error) {
console.error(error);
}
console.log(httpResponse.statusCode);
console.log(body);
});


Here I get httpResponse.statusCode as 403 - forbidden.
I was not expecting it because my $.ajax code works and does not return me this error.



What I've tried so far :




  • set user-agent as "node.js", "request", "Mozilla/5.0"

  • use axios ( also got the error 403 )

  • use request-promise


also I would like to know :




  • how to see both codes running to know whats the difference between each other

  • more user-agents, and which one I've to use to simulate a browser

  • if there is a better npm package to post data in 3rd part websites


Thanks in advance.










share|improve this question














I want to POST data in a 3rd part website through node.js but I am getting the error 403.
I have this code in browser :



$.ajax("/login",{ /// here i am in https://255.255.255 /// I am obscuring the real host ip

data:{
username:"username",
password: "password",
autologin:"true"},

method:"POST"

}).done(function(msg) {
console.log( msg );
/// another $.ajax request here
});


It works fine. The problem starts when I try to use it in node.js, I got this code after some help of SO users :



var request = require('request');

request({
method: "POST",
baseUrl: "https://255.255.255",
uri: "/login",
form: {
username: "username",
password: "password",
autologin: "true"
}
},
function (error, httpResponse, body) {
if (error) {
console.error(error);
}
console.log(httpResponse.statusCode);
console.log(body);
});


Here I get httpResponse.statusCode as 403 - forbidden.
I was not expecting it because my $.ajax code works and does not return me this error.



What I've tried so far :




  • set user-agent as "node.js", "request", "Mozilla/5.0"

  • use axios ( also got the error 403 )

  • use request-promise


also I would like to know :




  • how to see both codes running to know whats the difference between each other

  • more user-agents, and which one I've to use to simulate a browser

  • if there is a better npm package to post data in 3rd part websites


Thanks in advance.







javascript node.js http request header






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 13:55









HRPHRP

7310




7310













  • Maybe take a look at the response body of the failing HTTP request. It's possible that there's an error message.

    – Evert
    Jan 3 at 15:15











  • There is no error

    – HRP
    Jan 3 at 15:54



















  • Maybe take a look at the response body of the failing HTTP request. It's possible that there's an error message.

    – Evert
    Jan 3 at 15:15











  • There is no error

    – HRP
    Jan 3 at 15:54

















Maybe take a look at the response body of the failing HTTP request. It's possible that there's an error message.

– Evert
Jan 3 at 15:15





Maybe take a look at the response body of the failing HTTP request. It's possible that there's an error message.

– Evert
Jan 3 at 15:15













There is no error

– HRP
Jan 3 at 15:54





There is no error

– HRP
Jan 3 at 15:54












1 Answer
1






active

oldest

votes


















0














Does the jQuery code actually send the username and password as plaintext, or is it using Authorization? You can check in the browser console when you make the request. Typically user/password are sent as an Authorization header, you'll see something like "Authorization: Basic " in the headers of the request.



If so, you'll need to replicate that in Node as well. You can check out the request documentation for more information how - it's quite simple.






share|improve this answer
























  • I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

    – HRP
    Jan 3 at 18:28











  • Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

    – HRP
    Jan 3 at 21:09











  • Do not need anymore :D Thanks very much man

    – HRP
    Jan 3 at 21:29














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%2f54023711%2fhttp-request-in-node-js-returning-response-statuscode-as-forbidden%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









0














Does the jQuery code actually send the username and password as plaintext, or is it using Authorization? You can check in the browser console when you make the request. Typically user/password are sent as an Authorization header, you'll see something like "Authorization: Basic " in the headers of the request.



If so, you'll need to replicate that in Node as well. You can check out the request documentation for more information how - it's quite simple.






share|improve this answer
























  • I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

    – HRP
    Jan 3 at 18:28











  • Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

    – HRP
    Jan 3 at 21:09











  • Do not need anymore :D Thanks very much man

    – HRP
    Jan 3 at 21:29


















0














Does the jQuery code actually send the username and password as plaintext, or is it using Authorization? You can check in the browser console when you make the request. Typically user/password are sent as an Authorization header, you'll see something like "Authorization: Basic " in the headers of the request.



If so, you'll need to replicate that in Node as well. You can check out the request documentation for more information how - it's quite simple.






share|improve this answer
























  • I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

    – HRP
    Jan 3 at 18:28











  • Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

    – HRP
    Jan 3 at 21:09











  • Do not need anymore :D Thanks very much man

    – HRP
    Jan 3 at 21:29
















0












0








0







Does the jQuery code actually send the username and password as plaintext, or is it using Authorization? You can check in the browser console when you make the request. Typically user/password are sent as an Authorization header, you'll see something like "Authorization: Basic " in the headers of the request.



If so, you'll need to replicate that in Node as well. You can check out the request documentation for more information how - it's quite simple.






share|improve this answer













Does the jQuery code actually send the username and password as plaintext, or is it using Authorization? You can check in the browser console when you make the request. Typically user/password are sent as an Authorization header, you'll see something like "Authorization: Basic " in the headers of the request.



If so, you'll need to replicate that in Node as well. You can check out the request documentation for more information how - it's quite simple.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 16:35









Michael PrattMichael Pratt

2,4771126




2,4771126













  • I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

    – HRP
    Jan 3 at 18:28











  • Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

    – HRP
    Jan 3 at 21:09











  • Do not need anymore :D Thanks very much man

    – HRP
    Jan 3 at 21:29





















  • I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

    – HRP
    Jan 3 at 18:28











  • Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

    – HRP
    Jan 3 at 21:09











  • Do not need anymore :D Thanks very much man

    – HRP
    Jan 3 at 21:29



















I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

– HRP
Jan 3 at 18:28





I couldn't check, all I get in console while using ajax in browser is : ` {success: true, redirectTo: "255.255.255"}`

– HRP
Jan 3 at 18:28













Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

– HRP
Jan 3 at 21:09





Thanks. Your comment was actually very useful. I went to network tab in console then I saw my .ajax request and figured out that what is return 403 is their content-security-policy . Sadly I got a downvote and now I am banned from ask question SO. Could you suggest me a npm package where I can simulate an origin for my request ?

– HRP
Jan 3 at 21:09













Do not need anymore :D Thanks very much man

– HRP
Jan 3 at 21:29







Do not need anymore :D Thanks very much man

– HRP
Jan 3 at 21:29






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023711%2fhttp-request-in-node-js-returning-response-statuscode-as-forbidden%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter