i´m trying to get websites / xml objects from the huawei umts stick internal web gui.
I´ve written a wrapper in c using libcurl which works fine. Now i want to port this to node using request.
When i call a url with curl from console like this:
curl -G "http://192.168.1.1/api/monitoring/status"
I get normal result:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<ConnectionStatus>901</ConnectionStatus>
<SignalStrength>99</SignalStrength>
<SignalIcon>5</SignalIcon>
<CurrentNetworkType>4</CurrentNetworkType>
<CurrentServiceDomain>3</CurrentServiceDomain>
<RoamingStatus>0</RoamingStatus>
<BatteryStatus></BatteryStatus>
<BatteryLevel></BatteryLevel>
<simlockStatus></simlockStatus>
</response>
When i try to call this site with request:
var request = require("request");
var options = {
url: "http://192.168.1.1/api/monitoring/status"
};
request.get(options, function(error, response, body) {
if(error)
{
console.log(error);
}
console.log("statusCode: " + response.statusCode);
console.log("statusText: " + response.statusText);
console.log(body);
});
I got an 400 Error:
statusCode: 400
statusText: undefined
<HTML><HEAD>
<TITLE>IPWEBS - 400 Bad Request</TITLE>
</HEAD>
<BODY><H2>400 Bad Request</H2>
<P>The request generated an error response.</P>
</BODY>
</HTML>
Any idea on this subject?
It could be that curl is sending a header that the server requires. Try changing your options to:
var options = {
url: "http://192.168.1.1/api/monitoring/status",
headers: {
'User-Agent': 'curl/7.30.0',
'Accept': '*/*'
}
}
Related
I am trying to fetch the results from all repositories of my organisation under a search category. The results are fetched properly when I use the curl command as shown below
curl -H "Authorization: token ****" -i https://api.github.com/search/code?q=org:<org>+<search_param>
But when I try to run it programmatically in nodejs via the request module it is not returning any results.
My code is as shown below
const request = require("request");
const options = {
url:'https://api.github.com/search/code?q=org:<org>+<search_param>'
headers: {
"Autorization": "token ***",
"User-Agent": "request"
},
json:true
}
console.log(options);
request.get(options, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
The output for the above code is as below
body: {"total_count":0,"incomplete_results":false,"items":[]}
Please let me know what is wrong with the above code or if I am missing anything.
I was able to solve this by using the axios module instead of request module as the request module does not send the Authorization hader. Got a reference from Nodejs request module doesn't send Authorization header.
The updated code which works is as follows
const axios = require("axios");
const options = {
method:"get",
url:'https://api.github.com/search/code?q=org:<org>+<searchtoken>',
headers: {
"Authorization": "token ***",
"User-Agent": "abc"
}
}
console.log(options);
axios(options).then(function ( response) {
console.log('statusCode:', response); // Print the response status code if a response was received
// console.log('body:', body); // Print the HTML for the Google homepage.
}).catch(function (error) {
console.log(error);
});
Thanks #mehta-rohan for the help
I'm attempting to get a SOAP response from a web service without using any libraries in Node, but I'm getting nowhere with the code I have. The WSDL for the service can be found here. I've tested the request in SoapUI, and with curl in a batch file. The JavaScript:
const http = require('http')
const fs = require('fs')
const xml = fs.readFileSync('latlonzipcode.xml','utf-8')
const options = {
hostname : 'graphical.weather.gov',
path : '/xml/SOAP_server/ndfdXMLserver.php',
method : 'POST',
headers : {
'Content-Type' : 'text/xml;charset=utf-8',
'soapAction' : 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode'
}
}
var obj = http.request(options,(resp)=>{
let data = ''
console.log(resp.statusCode)
console.log(resp.headers)
resp.on('data',(chunk)=>{
data += chunk
})
resp.on('end',()=>{
console.log(data)
})
}).on('error',(err)=>{
console.log("Error: " + err.message)
})
obj.write(xml)
obj.end()
The SOAP envelope/XML file:
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ndf="https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<soapenv:Header/>
<soapenv:Body>
<ndf:LatLonListZipCode soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<zipCodeList xsi:type="xsd:string">90210</zipCodeList>
</ndf:LatLonListZipCode>
</soapenv:Body>
</soapenv:Envelope>
and the .bat file - for testing:
:: curl options: -H is for Header --data allows file parameters
curl -H "soapAction: \"https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListCityNames\"" -H "Content-Type: text/xml;charset=UTF-8" --data #latlonzipcode.xml https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php > response.xml
The response I receive from the service:
403
{ server: 'AkamaiGHost',
'mime-version': '1.0',
'content-type': 'text/html',
'content-length': '320',
expires: 'Wed, 18 Jul 2018 14:18:05 GMT',
date: 'Wed, 18 Jul 2018 14:18:05 GMT',
connection: 'close' }
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>
You don't have permission to access "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php" on this server.<P>
Reference #18.7fd23017.1531923485.f45e7526
</BODY>
</HTML>
The batch file works perfectly. Any help would be great. Thanks.
UPDATE
After Googling around I found this. So I added the header User-Agent : headerTest to my options... And finally got a response, unfortunately it was
the WSDL.
As stated in my update above, my original problem was due to not using https and that the web service was blocking requests without a User-Agent header. As for the WSDL file being returned as the response, I mistakenly used GET in my options and forgot to correct it. Finally I suspected that something was going on with writing the XML to the body and found that there was an issue - I found that answer here. Here is the corrected code:
const https = require('https')
const fs = require('fs')
const xml = fs.readFileSync('latlonzipcode.xml','utf-8')
const options = {
hostname : 'graphical.weather.gov',
port : 443,
path : '/xml/SOAP_server/ndfdXMLserver.php',
method : 'POST',
headers : {
'User-Agent' : 'sampleTest',
'Content-Type' : 'text/xml;charset=utf-8',
'soapAction' : 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode'
}
}
var obj = https.request(options,(resp)=>{
let data = ''
fs.writeFile('server.log',resp.statusCode+"\n"+JSON.stringify(resp.headers),(err)=>{
err ? console.log(err) : console.log('log file written')
})
resp.on('data',(chunk)=>{
data += chunk
})
resp.on('end',()=>{
fs.writeFile('soap-response.xml',data,(err)=>{
err ? console.log(err) : console.log('data file written')
})
console.log(data)
})
}).on('error',(err)=>{
console.log("Error: " + err.message)
})
/**
* '.write()' is not being used:
* obj.write(xml) ? console.log('op success') : console.log('error!')
* obj.end()
*/
obj.end(xml) ? console.log('op success') : console.log('error!')
Node.js has finally got me excited about JavaScript. Thanks.
I'm trying to use a sentiment analysis API for some tweets I've streamed. The API in question is this: http://sentiment.vivekn.com/docs/api/. I've done this before in Python before and it worked as expected. I made a post request using the requests library and sent a JSON object with my content. The JSON object looked something like this:
{
"txt": "The content of the tweet."
}
In Python, sending the post request looked something like this:
url = "http://sentiment.vivekn.com/api/text/"
data_dict = {
"txt": "hi"
}
r = requests.post(url,json.loads(json.dumps(data_dict)))
print(r.text)
Now I'll admit I'm new to Javascript and web based programming in general, but I assume the logic should be similar in both languages. I tried using the XMLHttpRequest method but it always returned an internal server error with status code: 500.
The website works, it takes post requests and responds with the analysis, but I can't get it to work with Node. This is what I'm working with in Javascript:
const rp = require('request-promise');
var options = {
method: 'POST',
uri: 'http://sentiment.vivekn.com/api/text/',
body: {
"txt": "This is a very negative sentence, so we should get a negative analysis!"
},
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function (parsedBody) {
console.log("Request received");
console.log(parsedBody);
})
.catch(function (err) {
console.log("Something went wrong\n" + err);
});
It always catches an error with status code 500. I've tried several other methods including making the request with XMLHttpRequest. Nothing seems to work. It would be great if someone could point out where I'm going wrong.
This isn't an answer, but I thought it useful to show some code that evokes a different response, which may be a clue that will help debug the problem.
I get the same response with curl:
jim-macbookpro:~/development/node/so$ curl -X POST 'http://sentiment.vivekn.com/api/text/' -H "Content-Type: application/json" -d '{"txt": "hi"}'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in theapplication.</p>
I changed the example to use 'node-fetch', and I don't get 500, rather I get 405 - METHOD NOT ALLOWED.
My suspicion is that this is a problem with the server being somehow very particular about the format of the request.
I hope this helps.
const fetch = require('node-fetch');
fetch('http://sentiment.vivekn.com/api/text', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
txt:
'This is a very negative sentence, so we should get a negative analysis!'
})
})
.then(function(parsedBody) {
console.log('Request received');
console.log(parsedBody);
})
.catch(function(err) {
console.log('Something went wrong\n' + err);
});
I'm sending the following post with FireFox rest client:
POST /WSPublic.asmx HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 462
Host: host
Connection: close
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><ContadoresTR xmlns="https://xxxxx"><Usuario>user</Usuario><Contrasena>password</Contrasena><NInstalacion>inst</NInstalacion><VersionFicheroEsperado>1</VersionFicheroEsperado></ContadoresTR></soap12:Body></soap12:Envelope>
It works, and I receive the response from the server.
Looking in Wireshark conversations, I see that firstly the RESTClient sends the following stream:
...........fe....0..S.... ..L0g....!...z.P. _....N.im3....8.q.'...6.9.....p>. .+./.
...........3.2.9./.5.
.......c........
ges.leako.com......
.................#..3t.....!...h2-14.spdy/3.1.spdy/3.http/1.1.........
Then I try to do the same using node.js. The HTTP client sends the same post and I don't receive response from the server. Looking at the Wireshark conversations, I see that the first HTTP Client stream the format is different from the RESTClient of firefox.
POST /WSPublic.asmx HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 462
Host: host
Connection: close
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><ContadoresTR xmlns="https://xxxxxx"><Usuario>user</Usuario><Contrasena>password</Contrasena><NInstalacion>inst</NInstalacion><VersionFicheroEsperado>1</VersionFicheroEsperado></ContadoresTR></soap12:Body></soap12:Envelope>
Here the node.js script:
var http = require("http")
var body = '<?xml version="1.0" encoding="utf-8"?>'
+'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
+'xmlns:xsd="http://www.w3.org/2001/XMLSchema"'
+'xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'
+'<soap12:Body>'
+'<ContadoresTR xmlns="https://ges.leako.com/WSPublic">'
+'<Usuario>xxx</Usuario>'
+'<Contrasena>xxx</Contrasena>'
+'<NInstalacion>xxx</NInstalacion>'
+'<VersionFicheroEsperado>x</VersionFicheroEsperado>'
+'</ContadoresTR>'
+'</soap12:Body>'
+'</soap12:Envelope>';
var postRequest = {
host: "ges.leako.com",
path: "/WSPublic.asmx",
port: 443,
method: "POST",
headers: {
'Content-Type': 'application/soap+xml; charset=utf-8',
'Content-Length': Buffer.byteLength(body),
'Connection': 'keep-alive'
}
};
var buffer = "";
var req = http.request( postRequest, function( res ) {
console.log( res.statusCode );
var buffer = "";
res.on( "data", function( data ) { buffer = buffer + data; } );
res.on( "end", function( data ) { console.log( buffer ); } );
});
req.write( body );
req.end();
What I'm doing wrong? Why Firefox send the POST format different than the node.js HTTP Client, and which is that format?
Thanks in advance.
Apart from the 'keep-alive' 'Connection' header and the 'Content-type' one, everything seems fine as mentioned by #mithunsatheesh in this other question how to post XML data in node.js http.request but I guess you were already aware of that since the code is pretty similar in both cases :)
I am using html on client side and my server side code is on node.js. I have some url's defined in to the nodejs application and I am calling them from my client html file. From node I am calling my REST application which is at another server. This rest application is written in Jersey java web services. I am able to call node.js from my html and from node code I am getting response from Jersey web service module. After receiving this response I am setting it to response object of node.js but this response is not available on html jquery ajax call.
$.ajax({
type :"POST",
dataType: 'json',
data: jsontest,
url: 'http://<code>localhost</code>:9999/hello',
success: function(data) {
console.log('success');
console.log(data);
console.log(data.id);
}, error : function(response) {
console.log(JSON.stringify(response));
}
});
Server side code:
var tmp = req;
var authentication = JSON.stringify(tmp.body.authenticationKey);
console.log("authentication :- "+authentication);
requestObj({
url : "http://<code>restserver</code>:port/restmodule/controller/hello",
method : "POST",
headers : { "Content-Type" : "application/json","pubKey":authentication},
body : JSON.stringify(tmp.body)
},
function (error, res, body) {
indexresponseBody = JSON.stringify(JSON.parse(body).message);
}
);
res.writeHead(200, {'content-type':'text/html'});
console.log("JSON returned from REST "+indexresponseBody);
res.write(indexresponseBody);
res.end();
I am able to get the json and this is printed on node server console. But when I am writing this json to the response(res) object on firebug console I am not able to see this json. Can anybody tell me how can I get this response.
Could be because of async nature of callback, try this -
function (error, resp, body) {
indexresponseBody = JSON.stringify(JSON.parse(body).message);
res.writeHead(200, {'content-type':'text/html'});
console.log("JSON returned from REST "+indexresponseBody);
res.write(indexresponseBody);
res.end();
}