Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

fetch() and YQL: mismatched character ' ' expecting '"'

ANSWERED

I want to retrieve Google News Headlines and display  them on Ionic and because Fitbit SDK can handle JSON, I'm going to rertrieve RSS feed in JSON format through YQL service.

 

I tested the following URL on Edge, IE11 and Chrome and the result  was what I wanted.

 

https://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22https%3A%2F%2Fnews.google.com%2Fnews%2Frss%2F%3Fned%3Djp%26hl%3Dja%22&format=json

 

 

But when I pass the URL to fetch() API like this,

 

var FEED_URL = "https://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22https%3A%2F%2Fnews.google.com%2Fnews%2Frss%2F%3Fned%3Djp%26hl%3Dja%22&format=json";
fetch(FEED_URL) .then(function (response) { response.text() .then(function(data) { console.log("Data: " + data); }); });

I cannot retrieve RSS feed and console.log() shows:

Data: {"error":{"lang":"en-US","description":"Query syntax error(s) [line 1:73 mismatched character ' ' expecting '\"']"}}

How do I fix my code?

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Problem solved.

 

I changed form sending method to POST,  and it works fine.

Fixed code is:

var YQL_URL = "https://query.yahooapis.com/v1/public/yql";
var GNEWS_FEED_URL = "https://news.google.com/news/rss/?ned=jp&hl=ja";

function urlEncodePost(object) {
    let fBody = [];
    for (let prop in object) {
        let key   = encodeURIComponent(prop);
        let value = encodeURIComponent(object[prop]);
        fBody.push(key + "=" + value);
    }
    fBody = fBody.join("&");
    return fBody;
};

function fetchNews() {
  fetch(YQL_URL, {
    method: "POST",
    headers: {
      "Accept":       "application/json",
      "Content-Type": "application/x-www-form-urlencoded"
    },
    body: urlEncodePost({
      "q":      "select title from rss where url=\"" + GNEWS_FEED_URL + "\"",
      "format": "json"
    })
  })
  .then(function (response) {
      response.text()
      .then(function(data) {
          console.log("Data: " + data);
      });
  });
};

 

View best answer in original post

Best Answer
0 Votes
1 REPLY 1

Problem solved.

 

I changed form sending method to POST,  and it works fine.

Fixed code is:

var YQL_URL = "https://query.yahooapis.com/v1/public/yql";
var GNEWS_FEED_URL = "https://news.google.com/news/rss/?ned=jp&hl=ja";

function urlEncodePost(object) {
    let fBody = [];
    for (let prop in object) {
        let key   = encodeURIComponent(prop);
        let value = encodeURIComponent(object[prop]);
        fBody.push(key + "=" + value);
    }
    fBody = fBody.join("&");
    return fBody;
};

function fetchNews() {
  fetch(YQL_URL, {
    method: "POST",
    headers: {
      "Accept":       "application/json",
      "Content-Type": "application/x-www-form-urlencoded"
    },
    body: urlEncodePost({
      "q":      "select title from rss where url=\"" + GNEWS_FEED_URL + "\"",
      "format": "json"
    })
  })
  .then(function (response) {
      response.text()
      .then(function(data) {
          console.log("Data: " + data);
      });
  });
};

 

Best Answer
0 Votes