Getx: Decoding the response in JSON before calling the defaultDecoder

Created on 1 Dec 2020  路  6Comments  路  Source: jonataslaw/getx

https://github.com/jonataslaw/getx/blob/fb1f511f646cce5bb05bb3917726859ddc9a68c4/lib/get_connect/http/src/http_impl/http_request_io.dart#L61

Is this right?

I'm calling an rss url that responds with xml, then a try to parse the response using webfeed package

class HomeProvider extends GetConnect implements IHomeProvider {
  @override
  void onInit() {
    httpClient.defaultDecoder = HomeProvider.parseRss;
    httpClient.baseUrl = 'https://www.pokemon.com/us/pokemon-news/';
  }

  @override
  Future<Response<RssFeed>> getNews() => get("rss");

  static RssFeed parseRss(dynamic xml) => RssFeed.parse(xml);
}

but the response.body fails to execute RssFeed.parse becouse its trying to decode xml into json.

enhancement

All 6 comments

Currently GetConnect is only prepared for API endpoints that return a JSON. I will mark this issue as a suggestion for improvement to also be able to obtain returns other than JSON

I just switched

body = request.decoder(jsonDecode(stringBody)); 

by

body = request.decoder(stringBody); 

and it worked perfectly (with XML), my custom decoder was able to decode the content and return my custom object.

It's not the case?

If I have a custom decoder, does not make sense use jsonDecode before the custom decoder in my opinion.

I understand your point of view, but in most scenarios we work with APIs that return JSON, so GetConnect wanted to make this easier. Now an improvement is needed to allow working with other types of responses, but in my opinion it would not be removing jsonDecode but creating a property to inform that the endpoint does not return JSON

Alright, I agree.

Can we do that looking for the Content-Type of response?

Hi @eduardocp, update GetX to version 3.21.3, it resolves this issue

As this was corrected two versions ago, I'm closing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manojeeva picture manojeeva  路  3Comments

aztecrabbit picture aztecrabbit  路  3Comments

DarkHeros09 picture DarkHeros09  路  3Comments

jemariads picture jemariads  路  4Comments

definev picture definev  路  3Comments