0

I'm trying to use opengraph.io in the client side. They have docs for jQuery which I've based my code on: https://www.opengraph.io/examples/jquery-opengraph-example/

      const url = 'http://cnet.com';
      const urlEncoded = encodeURIComponent(url);
      const apiKey = 'MY-KEY';
      const requestUrl = 'https://opengraph.io/api/1.1/site/' + urlEncoded + '?app_id=' + apiKey;
      fetch(requestUrl).then(res => {
        console.log(res);
        console.log(res.body);
      });

The response I get from res appears to be successful but doenst have the data I need.

body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: ""
type: "cors"
url: "https://opengraph.io/api/1.1/site/http%3A%2F%2Fcnet.com?app_id=MY-KEY"

I tried logging out res.body but it just returns:

ReadableStream {locked: false}

I found a similar question here but the API seems to return res.data which isnt the case for me:

How to get Open Graph meta from URL input on the client side

Evanss
  • 23,390
  • 94
  • 282
  • 505

1 Answers1

0

You'll need to add res.json() to convert the ReadableStream into some JSON.

For example, with an invalid key, just to show the output:

const url = 'http://cnet.com';
const urlEncoded = encodeURIComponent(url);
const apiKey = 'MY-KEY';
const requestUrl = 'https://opengraph.io/api/1.1/site/' + urlEncoded + '?app_id=' + apiKey;

fetch(requestUrl)
  .then(res => res.json())
  .then(json => console.log(json));
0stone0
  • 34,288
  • 4
  • 39
  • 64