how to send xml response to a given url from express route? - node.js

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);
});

Related

How can I improve the elegancy of my code that uses axios?

I am trying to migrate from request to axios, since request has been deprecated.
Suppose that the url 'https://www.example.com' receives a post request with formdata that contains login information, and also suppose that I need to maintain the current session across multiple requests(for that I need a cookieJar).
Using axios I need cookieJar support from an external library therefor I use axios-cookiejar.
Also to send formdata using axios I have to use the external library form-data, and I also have to set the headers manually since axios doesn't do that for me.
I have the following code, which uses axios that does just that:
axios = require('axios')
FormData = require('form-data')
axiosCookieJarSupport = require('axios-cookiejar-support').default
tough = require('tough-cookie')
axiosCookieJarSupport(axios)
cookieJar = new tough.CookieJar()
form = new FormData()
form.append('email', 'example#gmail.com')
form.append('password', '1234')
axios({
method: 'post',
url: 'https://www.example.com',
data: form,
headers: {'Content-Type': `multipart/form-data; boundary=${form._boundary}` },
jar: cookieJar,
withCredentials: true
}).then(function (response) {
console.log(response['data'])
})
Using request this becomes much simpler:
request = require('request')
requestCookieJar = request.jar()
request.post({
url: 'https://www.example.com',
method: 'POST',
jar: requestCookieJar,
formData: {
'email': 'example#gmail.com',
'password': '1234'
}
}, function(error, response, body) {
console.log(body)
})
As you can see the request API is much more elegant.
Is there a way to use axios more elegantly or is there a different elegant API, which isn't deprecated, that I could use to support my needs stated at the beginning of this question?

override the content-type being set in the req.header with node js?

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);
});
};

How to view request sent from node.js to server?

Regarding to this question:
Transfer / pass cookies from one request to another in nodejs/protractor
I got another one. How could I view complete request (headers + body) which I am performing via nodejs?
Yes, you can ... You can access the complete request from the full response body - response.request
I have a generic full response structure illustrated below
IncomingMessage
ReadableState
headers(ResponseHeaders)
rawHeaders
request - //This is what you need
headers
body
body(Response Body)
You can access through code as shown below
var request = require("request");
var options = { method: 'POST',
url: 'http://1.1.1.1/login',
headers:
{ 'cache-control': 'no-cache',
'content-type': 'application/json' },
body: { email: 'junk#junk.com', password: 'junk#123' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
// This will give you complete request
console.log(response.request);
//This gives you the headers from Request
console.log(response.request.headers);
});

RequestJS pass a content-type: application/json

I'm looking a way to pass a content-type in RequestJS using with Nodejs.
Right now I have this as params:
'use strict';
request = require('request'),
app.register = function(req, res) {
request.post({
headers: {'Content-Type' : 'application/json'},
url: 'my.url.here',
form: req.body,
}).pipe(res);
}
But for some reason the server it still says it's a content-type text/xml..
Anyone can tell me how to customize this?
You should use the json option, not form, and you don't need to set the content type, the json option does that for you.
request.post({
url: 'my.url.here',
json: req.body
}).pipe(res);

Angular.js : how to use $http as request module node.js

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

Resources