ElliottLandsborough / dog-ceo-api

The API hosted at dog.ceo
https://dog.ceo/api
630 stars 66 forks source link

Endpoint for get image directly #40

Open cncolder opened 2 years ago

cncolder commented 2 years ago

Currently, all endpoints responds a json. Is there a way to get the image directly?

e.g. https://dog.ceo/api/breeds/image/random.jpg

So that we can embed in markdown:

![](https://dog.ceo/api/breeds/image/random.jpg)
iamabhshk commented 1 year ago
const axios = require('axios');
const url = 'https://dog.ceo/api/breeds/image/random.jpg';
axios.get(url, { responseType: 'blob' })
  .then((response) => {
    if (response.status === 200) {
      const blob = response.data;
      const image = document.createElement('img');
      image.src = URL.createObjectURL(blob);
      document.body.appendChild(image);
    } else {
      console.log('Error getting image.');
    }
  })
  .catch((error) => {
    console.log(error);
  });

Hope, I answered your question