Bad request when using axios to access google image search API Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Bad request when using axios to access google image search API without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want to search google for three images to display on my site.

I thought this was the minimum info the api needed to get the information but maybe I’m missing something. Google isn’t being very helpful on this.

const GOOGLE_API_URL = 'https://www.googleapis.com/customsearch/v1';

async function google_image_search(searchTerm){
  const response = await axios({
    method:'GET',
    url:GOOGLE_API_URL,
    params:{
      q:searchTerm,
      num:3,
      searchType:'image',
      key:API_KEY,
    }
  });
  return response;
}
{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "errors": [
            {
                "message": "Request contains an invalid argument.",
                "domain": "global",
                "reason": "badRequest"
            }
        ],
        "status": "INVALID_ARGUMENT"
    }
}

Answer

I figured it out. I had to set it to images in the control panel of the custom search engine in my google developer settings.

We are here to answer your question about Bad request when using axios to access google image search API - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji