简体   繁体   中英

Javascript Request--> Get API from https

I need to connect a database to a website and I have already done this with python for my application, but it seems hooking it up with javascript is going to be much harder.

Both headers and parameters are filled with key:value pairs with the information necessary.

Here is my code that I have for python:

response = requests.get("https://websiteAPI", headers=headers, params=parameters)

I need to be able to change the headers, as that is where I have my password for the api stored and also change the parameters so that I am able to get what I need.

Currently I am using the request library from javascript and have this, but I am still getting numerous errors from this.

var response = new request.Request("https://websiteAPI", {headers: headers} );

Is there something that I am missing to make the hookup to javascript as easy as it is in python?

My solution to this was using fetch with documentation found here.

var fetchUrl = require('fetch').fetchUrl

var response = fetchUrl("https://websiteAPI/?limit = 25&offset = 0", {headers:{"code" : "code"}}, function(error, meta, body){
    console.log(body.toString());
});

Add any parameters after the ?in the URL and seperate them by &, then add information needed for headers in the headers section. Still a bit more messy than python but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM