Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Created May 30, 2017 03:09
Show Gist options
  • Save xgqfrms-GitHub/d8afbd6484cf10da3603c6f51d6d4907 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/d8afbd6484cf10da3603c6f51d6d4907 to your computer and use it in GitHub Desktop.

newsapi

https://newsapi.org/

https://newsapi.org/sources

Key:

0987b00e542141369049e647701b65d9

Parameters

source (required) - The identifer for the news source or blog you want headlines from. Use the /sources endpoint to locate this or use the sources index.

apiKey (required) - Your API key. Alternatively you can provide this via the X-Api-Key HTTP header.

sortBy (optional) - Specify which type of list you want. The possible options are top, latest and popular. Note: not all options are available for all sources. Default: top. top Requests a list of the source's headlines sorted in the order they appear on its homepage. latest Requests a list of the source's headlines sorted in chronological order, newest first. popular Requests a list of the source's current most popular or currently trending headlines.

https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=0987b00e542141369049e647701b65d9

Parameters (no key)

category (optional): business, entertainment, gaming, general, music, politics, science-and-nature, sport, technology. Default: empty (all sources returned)

language (optional) : en, de, fr. Default: empty (all sources returned)

country (optional) : au, de, gb, in, it, us. Default: empty (all sources returned)

https://newsapi.org/v1/sources?language=en&country=us&category=technology

demo

fetch('https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest',{
    method: 'get',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'X-Api-Key': `0987b00e542141369049e647701b65d9`
    }
})
.then(function(response) {
    return response.json()
}).then(function(json) {
    console.log('parsed json: ', json)
}).catch(function(error) {
    console.log('parsing failed: ', error)
});



fetch('https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=0987b00e542141369049e647701b65d9')
.then(function(response) {
    return response.json()
}).then(function(json) {
    console.log('parsed json: ', json)
}).catch(function(error) {
    console.log('parsing failed: ', error)
});

https://gist.github.com/xgqfrms-GitHub/b44ba9049575654e36e3032c58576931

GET/HEAD Error

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented May 30, 2017

you don't konw github

github-gist-embed

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

fetch('https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest',{
    method: 'get',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'X-Api-Key': `0987b00e542141369049e647701b65d9`
    }
})
.then(function(response) {
    return response.json()
}).then(function(json) {
    console.log('parsed json: ', json)
}).catch(function(error) {
    console.log('parsing failed: ', error)
});

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 3, 2017

HTTP Headers

https://gist.github.com/xgqfrms-GitHub/636260e8332f0fe4f530fe040ffd3741

// https://api.mlab.com/api/1/databases/node-mongodb/collections/test?apiKey=pa4Yku0O7y6CHLqKwDGlLKSfkdPfQprR
const url = `https://api.mlab.com/api/1/databases/node-mongodb/collections/test?apiKey=pa4Yku0O7y6CHLqKwDGlLKSfkdPfQprR`;

fetch(url, {
    method: "PUT",
    body: JSON.stringify({}),
    headers: {
        'Accept': 'application/json',
        'Access-Control-Request-Method': 'PUT',
        'Content-Type': 'application/json'
    },
    credentials: "same-origin"
}).then(function(response) {
    response.status; //=> number 100–599
    response.statusText; //=> String
    response.headers; //=> Headers
    response.url; //=> String

    return response.text()
}, function(error) {
    error.message; //=> String
})

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 3, 2017

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

HTTP Headers

https://gist.github.com/xgqfrms-GitHub/636260e8332f0fe4f530fe040ffd3741

// https://api.mlab.com/api/1/databases/node-mongodb/collections/test?apiKey=pa4Yku0O7y6CHLqKwDGlLKSfkdPfQprR
const url = `https://api.mlab.com/api/1/databases/node-mongodb/collections/test?apiKey=pa4Yku0O7y6CHLqKwDGlLKSfkdPfQprR`;

fetch(url, {
    method: "PUT",
    body: JSON.stringify({}),
    headers: {
        'Accept': 'application/json',
        'Access-Control-Request-Method': 'PUT',
        'Content-Type': 'application/json'
    },
    credentials: "same-origin"
}).then(function(response) {
    response.status; //=> number 100–599
    response.statusText; //=> String
    response.headers; //=> Headers
    response.url; //=> String

    return response.text()
}, function(error) {
    error.message; //=> String
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment