0

I want to use an API (that doesn't support CORs) in my NodeJS app.

Actually it's not really an API. A site uses that link to get the data.

I have downloaded a browser extension which lets me access the API. Is there any way I can access it in my NodeJS app, like by tweaking the Http header?

I use request module to send request.

var request = require('request');

var options = {
    url: 'http://kalimatimarket.gov.np/priceinfo/dlypricebulletin',
    method: 'POST',
    data : "cdate=07/01/2017&pricetype=R"
    // do something to allow cors
};

request(options, (err, res, body)=> {
    console.log(body)
});

Without and with CORS enabled

Aditya
  • 677
  • 2
  • 9
  • 15
  • If its on serverside theres no need to. – Jonas Wilms Jul 01 '17 at 16:47
  • We're confused. The code you show is server-side code (runs in node.js, not in a browser), yet the screen grabs you show are browser errors. There are no cross origin restrictions when making requests from a server or from node.js. Cross origin restrictions ONLY apply to requests made in a browser as the whole notion of cross origin restrictions is something that a browser implemented and only applies to Javascript run in a browser. – jfriend00 Jul 01 '17 at 17:09
  • If you are getting errors when running this code in node.js, then please show us those errors so we can help you deal with those. – jfriend00 Jul 01 '17 at 17:10
  • @jfriend00 I am not getting an error. The thing is I'm not getting the data. When a request is made to the url it gives one of the two responses i. Response with the data ii. Response with Php Error & some HTML. When requesting from my node app I get the second response – Aditya Jul 01 '17 at 17:21
  • [Response](http://imgur.com/a/ei0QM) – Aditya Jul 01 '17 at 17:23
  • Then you need to study the request being made by the site from the browser in the network tab of the Chrome debugger and see what is in that request that you don't have. It could be custom headers, it could be cookies, it could be POST data. You are missing something that makes the site think your request is different than the one they send from their web page. It occurs to me that what you are attempting to do may be a violation of Terms or Service since it is apparently not intended as a publicly accessible API. – jfriend00 Jul 01 '17 at 17:25
  • [This Worked for me!](https://stackoverflow.com/a/24650924/8230380) – Aditya Jul 01 '17 at 18:14

1 Answers1

1
  • Either set Allow-Control-Allow-Origin on the server site indicating the client url, or
  • Use jsonp
vahdet
  • 6,357
  • 9
  • 51
  • 106