Ionic 3 RSS read with rss2json “Unprocessable Entity”

后端 未结 1 1985
清酒与你
清酒与你 2021-01-27 18:45

I\'m having trouble converting RSS to JSON using the rrs2json API with Ionic 3. If I execute the code it gives me the error --> Response {_body: \"{\" status \":\" error \",\" m

1条回答
  •  一向
    一向 (楼主)
    2021-01-27 18:49

    Alright. I registered myself with the rss2json service and made sure this solution actually works (you can see the data in console).

    The issue you have is that you are not using a proper way to form http request with HttpParams.

    Here is working stackblitz that uses my key: https://stackblitz.com/edit/ionic-jdwqjg

    now some details:

    • when you configure a URL using rss2json it basically adds parameters to the original URL, example:

    https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Ftechcrunch.com%2Ffeed%2F&api_key=q5ijkolkdjk3urzrcfaehxeoimxr3tdu5ieiqcrq&order_by=pubDate&order_dir=asc&count=20

    • So in Angular/Ionic you need to leverage Angular's HttpParams to properly form request, here is your provider code with HttpParams:

    provider code:

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpParams } from '@angular/common/http';
    
    @Injectable()
    export class RssProvider {
    
      private API_URL: string;
    
      constructor(public http: HttpClient) {
        this.API_URL = "https://api.rss2json.com/v1/api.json";
      }
    
      GetRSS() {
        const params = { params: new HttpParams().set('rss_url', 'http://rss.cnn.com/rss/edition.rss').set('api_key','q5ijkolkdjk3urzrcfaehxeoimxr3tdu5ieiqcrq').set('order_by', 'pubDate').set('order_dir', 'asc')
        }
        return this.http.get(this.API_URL, params);
      }
    
    }
    

    0 讨论(0)
提交回复
热议问题