0

I have a GET request Axios:

const fetchData = () => {
        axios( {
            method: "GET",
            url: inputData.cUrl,
            headers: { "content-type": "text/html" }
        } ).then( ( response ) => {
            const parser = new DOMParser();
            const doc = parser.parseFromString( response, "text/html" );
            console.log( doc );
        } ).catch( e => console.log( e ) );
    };

However, if inputData.cUrl is "www.google.com", the request will fail with the error message of

"GET http://localhost:3000/www.google.com 400 (Header required)".

2 questions.

Why does it append the url to the end of my localhost url? I want the get request to only go to the url the user puts in, not appended to my localhost.

And what does (Header required) mean? Never seen this with axios before.

Thank you for your help!

6
  • 1
    "Why does it append the url to the end of my localhost url?" - because you used a relative URL, instead of a proper absolute one. Commented Nov 24, 2019 at 12:30
  • @04FS How do I make it absolute in the axios request? The only thing I have in the url property in the request is the url I want Commented Nov 24, 2019 at 12:31
  • 1
    Well then you will have to modify the value to add what is missing. (And probably check first, if it is really missing - if the user input it already, that would error out in the other direction otherwise.) Commented Nov 24, 2019 at 12:32
  • Even if i hardcode the value to be "www.google.com" it still goes to "localhost/www.google.com" Commented Nov 24, 2019 at 12:34
  • Yes, it was just for testing. I fixed it now, adding the correct protocol in the beginning of the URL. Thank you for your guidance! Commented Nov 24, 2019 at 12:36

1 Answer 1

2
  1. You need to add the http protocol in front of the URL. Here you go.

const inputData = {};
inputData.cUrl = 'https://www.google.com';
const fetchData = () => {
  axios({
      method: 'GET',
      url: inputData.cUrl,
      headers: {
        'content-type': 'text/html'
      }
    })
    .then(response => {
      const parser = new DOMParser();
      const doc = parser.parseFromString(response, 'text/html');
      console.log(doc);
    })

    .catch(e => console.log(e));
};

  1. you can ignore this because the Request was going to your server instead of 'www.google.com'
Sign up to request clarification or add additional context in comments.

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.