Before using Angular.js's $http, I was testing the website's reactions with requests from the request module of node.js.
My goal was to log in to a website with a POST request, take the cookie session and then download all the pages I wanted with requests with this cookie in their headers.
With Node.js, it looked like this :
request.post({url: myUrl/login.php, form:{login:'login', password:'password'}}, function(err, resp, body){
var cookies = parseCookies(resp) //function that takes all the cookies
request({url: myUrl/portail, headers{cookie: cookies}}, function(err, resp, body){console.log('recu')})
})
The response to post request is actually a redirection to myUrl/portail with 'set-cookie' in the headers
I would like to do this with my app using Angular.js, so I did this :
$http({
method: 'POST',
url: myUrl/login.php,
data: $.param({login: $scope.loginData.login, password: $scope.loginData.password}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers) {
var cookies = parseCookieAngularVersion(headers)
$http({
method:'GET',
url: myUrl/portail,
headers:{cookie:cookies}
})
.success(function(data, status, headers){
console.log(headers())
})
.error(function(){
console.log('ERRRROR')
})
})
.error(function(data, status, headers, config) {
console.log('ERRROR');
})
But it doesn't work... The headers don't contain any cookie and the server keeps send me the login page. The problem must be because AngularJs is a client-side application.
Could someone helps me ? I would be very great. Thanks in advance
Related
I am attempting to get data in the form of an image sent from elsewhere using multipartform, however when trying to understand this via the great sanctuary(stack overflow) there are missing elements I don't quite understand.
const options = {
method: "POST",
url: "https://api.LINK.com/file",
port: 443,
headers: {
"Authorization": "Basic " + auth,
"Content-Type": "multipart/form-data"
},
formData : {
"image" : fs.createReadStream("./images/scr1.png")
}
};
request(options, function (err, res, body) {
if(err) console.log(err);
console.log(body);
});
2 questions:
what is the variable auth, what do I initialize it to/where/how do I declare it
what is the url "api.LINK.com", is this just the site url where this code is on
After your comments I think I may be doing this wrong. The goal is to send data(an image) from somewhere else(like another website) to this node app, then the nodeapp uses the image and sends something back.
So that I would be the one creating the API endpoint
I need to send a XML response from express route to an url given as a POST Request?
const xml = `<sourcedGUID>
<sourcedId>ASSMT12345</sourcedId>
</sourcedGUID>
<contextID>
<textString>cls1234</textString>
</contextID>
<userID>
<textString>usr123</textString>
</userID>`
I am in express route of /status and I need to send the xml variable to the url: 'https://example.com/cli' using express.
How can I achieve this use case?
You need to use something like request or axios or http, I will use request here
var request = require('request');
request.post({
url: <the-url-where-to-send-xml>,
method: 'POST',
headers:{
'Content-Type': 'application/xml',
},
body: xml
},
function(error, response, body){
console.log(response.statusCode);
console.log(body);
console.log(error);
});
I have a web service making a post request to an API and in the process, for some reason, the content-type value of the header is being overwritten from 'application/json' to 'text/html'. This is causing the POST request to fail since the API only accepts the content-type: 'application/json'. To overcome this I was going to make the web service touch a proxy web server that would implement the server-side code to modify the req.header value "content-type" back to 'application/json' and send the post request along with the req.body and req.headers to the API. I am trying to do this in node js (with express js). How do i override the req.header on the proxy node js server? I have tried playing with the accept content-type to only application/json but that did not do what i needed :(
let request = require('request');
let proxyRequest = (data,headers)=>{
var options = {
'method': 'POST',
'url': 'your-api-url',
if (headers.hasOwnProperty('Content-Type')){
delete headers['Content-Type']
}
headers['Content-Type']= 'application/json'
body: data
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
};
I have a simple NodeJS app which authenticates with remote Java CMS. On successful authentication the CMS returns a cookie.
However, for the life of me I can't figure how to access / get this cookie value. (I need the cookie in order to make requests to the CMS's API, once authenticated).
I get the cookie returned in a curl command ok but can't access it in NodeJS.
Curl Command:
curl -v -k --data "username=admin&password=password111&realm=cms101"
https://test.abeo.ie/gatekeeper/rs/authenticate/login
Now here is my NodeJS app's code:
//disable self-signed ssl cert rejection
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var http = require('http');
var request = require('request');
var Cookies = require('cookies');
//enable cookies
request.defaults({jar: true});
request({
url: 'https://test.abeo.ie/gatekeeper/rs/authenticate/login', //URL to hit
qs: {username: 'admin', password: 'password111', realm: 'cms101'}, //Query string data
method: 'POST', //Specify the method
headers: { //We can define headers too
'Content-Type': 'application/x-www-form-urlencoded'
}
},
function(error, response, body){
if(error) {
console.log(error);
} else {
//response.cookie comes out as 'undefined'
console.log(response.statusCode, body, response.cookie);
}
})
So my question is: How do I read the cookie value from the response as the response.cookie value prints out as undefined.
Thanks,
Mike
** If this question seems to vague... Just say so and I will clear it up.
I think I have just answered my question :)
I had the syntax wrong and instead of
console.log(response.statusCode, body, response.cookie);
I needed
console.log(response.statusCode, body, response.headers['set-cookie']);
Just working on parsing the thing now :)
hye,
i am building an app with angular.js and node.js (Express.js) on the server side.
for some reason i am having a problem handling a delete request. no body is getting to the server side.
this is my angular.js resource code:
$scope.deleteProject = function(projectName){
var postData = {username: 'name', projectName: projectName};
Project.deleteProject.delete({}, postData,
function(res){
alert('Project Deleted');
},
function(err){
alert(err.data);
});
}
on the server side i have this:
var deleteProject = function(req, res){
console.log(req.body);
console.log(req.params);
if (req.body.projectName){
//do something
return res.send(200);
}
else
return res.send(400, 'no project name was specified');
}
now for some reason there is no body at all!! it is empty.
i have defined the route as app.delete.
if i change the route in node.js to post and in angular.js to save it works fine.
what am i missing here (banging my head).
thanks.
As per this stack overflow question and the $http service source code, a DELETE request using $http does not allow for data to be sent in the body of the request. The spec for a DELETE request is somewhat vague on whether or not a request body should be allowed, but Angular does not support it.
The only methods that allow for request bodies are POST, PUT, and PATCH. So the problem is not anywhere in your code, its in Angular's $http service.
My suggestion would be to use the generic $http(...) function and pass in the proper method:
$http({
method: 'DELETE',
url: '/some/url',
data: {...},
headers: {'Content-Type': 'application/json;charset=utf-8'}
})
Angular by default sends the Content-Type as text/plain for DELETE requests. Just add this to the headers:
var config = {
method: "DELETE"
url: yourUrl
data: yourData
headers: {"Content-Type": "application/json;charset=utf-8"}
};
$http(config);
If you want to add them to every single DELETE request add this to the app.config method in your main controller:
$httpProvider.defaults.headers.delete = { "Content-Type": "application/json;charset=utf-8" };
If you want to use the $resource object instead of $http you need to add hasBody and headers as follow:
delete: {
method: 'DELETE',
hasBody: true,
headers: {"Content-Type": "application/json;charset=UTF-8"}
}
Worked for me
Just ran into this problem. You'll have to use url params to send an id with delete.
in express:
app.delete('/api/user/:userId', user.remove);
and add to the url in angular:
$http({url: 'whatever/api/'+obj.id, method: 'DELETE'}) ...
The following works for me:
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';
XMLHttpRequest is optional but useful if you are sending ajax.
https://docs.angularjs.org/api/ng/provider/$httpProvider for more information.
This worked for me.
$httpProvider.defaults.headers.delete = { "Content-Type": "application/json;charset=utf-8" };
And then
$http.delete(url, { data: data })