-1

I am working a small webapp to teach myself some gcloud and js. currently am trying to send a get request from a google cloud function using axios to a web api. any guidance would be greatly appreciated.

the web api needs the following parameters in the header Accept:application/json APIKey: APIKEY

I've been trying to use the code in How to set header and options in axios? and axios info, but I keep getting errors (SyntaxError: Unexpected token)

Index.JS currently looks like so

const axios = require("axios");

exports.run = async(req, res) => {

    axios.get('APIURL', {
     headers: {
       'Accept': 'application/json';
       'APIKey': 'KEY'
     }
    }).then((response) => {
        res.status(200).send(response.data);
        console.log(response);
      }, (error) => {
        res.status(500).send(response.data);
        console.log(error);
      });

     };

package.json

{
  "name": "YOUR_NAME",
  "version": "0.0.1",
  "dependencies": {
    "axios": "^0.19.2"
  }
}
Rob
  • 14,746
  • 28
  • 47
  • 65
slair
  • 39
  • 6

1 Answers1

3

This is not a valid JSON object:

     headers: {
       'Accept': 'application/json';
       'APIKey': 'KEY'
     }

The semicolon is out of place. Use a comma instead:

     headers: {
       'Accept': 'application/json',
       'APIKey': 'KEY'
     }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • That's not valid JSON either. Property names must be strings, not identifiers and strings must be quoted with double, not single, quotes. – Quentin Jun 01 '20 at 21:56
  • … and looking at the context, [it isn't supposed to be JSON anyway](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Quentin Jun 01 '20 at 21:56
  • Thanks for the inputs! I could have sworn I had a comma there. I did format the code in notepad before creating this post so I may have just typed it in afterwards. sorry for the confusion But I double checked everything, still getting errors, my issue is that am not sure how to pass header info...and first time using java, a lot of new things but jumping in head first. Error Looks like so Error: function terminated. Function cannot be initialized. – slair Jun 03 '20 at 20:35