0

I need to call the propublica API the call example they have is using cURL:

  -H "X-API-Key: PROPUBLICA_API_KEY" 

How can i rewrite it in axios.

I tried this and does not work, get an undefined response.

    axios.get('"https://api.propublica.org/congress/v1/members/{house}/{FL}/current.json/X-API-Key/APIKEY '),

]).then(axios.spread((response1, response2) => {
    console.log(response1.data.url);

})).catch(error => {
    console.log(error);
}); ```

3 Answers 3

1

the -H option is to pass the option as a header, not as a query parameter or as a part of the URL. You would have to do something like this instead:

axios.get(url, { headers: { 'X-API-Key': headerKey } })
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying with this: ``` const axios = require('axios'); axios.all([ axios.get('"api.propublica.org/congress/v1/115/senate/members.json"', { headers: { 'X-API-Key': "9q67HMZ2ly0hOrXWjT9l31WN6TiGuh7XrkBl1vC"} }) ]) .then(axios.spread((response) => { console.log(response.data.url); })).catch(error => { console.log(error); }); ``` still getting undefined
What Mario-F and sairtun proposed is the right way to do it. Also use headers instead of params.
0

Using the RESTClient Extension for firefox, this worked for me:

https://api.propublica.org/congress/v1/116/senate/members.json

with in Header X-API-Key:my-personal -key. So using axios, you may use:

let url = 'https://api.propublica.org/congress/v1/116/senate/members.json';
axios.get(url,
  { 
    headers: { 
      'X-API-Key': headerKey 
    }
  }
)
.then (res=>console.log(res))   
.catch(err => console.log(err));

1 Comment

You are welcome sairtun. Glad to help you.
0

This is working, thanks for the answers.

const axios = require('axios');

axios.request({
    url: "https://api.propublica.org/congress/v1/members/house/FL/current.json",
    headers: { 'X-API-Key': "API-KEY" },
    method: 'get'
}).then(response => {
    // console.log(response.data.url);
    console.log(response.data)
}).catch(error => {
    console.log(error);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.