I have a REST API that I need to access from a Flutter app. It is from the Spotify Web API. The terminal command that I would use for this is

curl -X "GET" "https://api.spotify.com/v1/me/following?
type=artist&limit=50" -H "Accept: application/json" -H "Content-Type: 
application/json" -H "Authorization: Bearer (ACCESS TOKEN)"

Which works. In Flutter, I have imported the import 'package:http/http.dart' as http package and import 'dart:convert'. I also have a Future that is as follows

Future<String> getData() async {
    http.Response response = await http.get(
        Uri.https("api.spotify.com", "v1/me/following?type=artist&limit=50"),
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json",
          "Authorization":
              "Bearer (ACCESS TOKEN)"
        });

    Map dataMap = jsonDecode(response.body);
    print(dataMap);
  }

This results in {error: {status: 404, message: Service not found}} which is weird since it works perfectly in terminal. What am I doing wrong?


Solution 1: Akif

You need to keep it simple. There might some problem related to URL. Try this:

Future<String> getData() async {
    http.Response response = await http.get(
        "https://api.spotify.com/v1/me/following?type=artist&limit=50",
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/json",
          "Authorization":
              "Bearer (ACCESS TOKEN)"
        });

    Map dataMap = jsonDecode(response.body);
    print(dataMap);
  }


Solution 2: Ουιλιαμ Αρκευα

NetService:

class NetService {
  static Future<T?> getJson<T>(String url, {int okCode = 200, String? authKey, Map<String, String>? headers}) {
    var localHeaders = <String, String>{};

    localHeaders['Accept'] = 'application/json';
    localHeaders['Content-Type'] = 'application/json';
    if (authKey != null) localHeaders['Authorization'] = 'Bearer $authKey';
    if (headers != null) localHeaders.addAll(headers);

    return http.get(Uri.parse(url), headers: localHeaders)
      .then((response) {
        if (response.statusCode == okCode) {
          return jsonDecode(response.body) as T;
        }
        PrintService.showDataNotOK(response);
        return null;
      })
      .catchError((err) => PrintService.showError(err));
  }
}

Main:

import 'dart:async';

import 'package:_samples2/networking.dart';

class Spotify {
  static const _oAuthToken = 'XXXXYYYYYZZZZ';
  static const _url = 'https://api.spotify.com/v1';

  static FutureOr<void> getUsersFollowedArtists() async {
    await NetService.getJson<Map<String, dynamic>>(_url + '/me/following?type=artist&limit=50', authKey: _oAuthToken)
      .then((response) => print(response))
      .whenComplete(() => print('\nFetching done!'));
  }
}

void main(List<String> args) async {
  await Spotify.getUsersFollowedArtists();
  print('Done.');
}

Result:

{artists: {items: [], next: null, total: 0, cursors: {after: null}, limit: 50, href: https://api.spotify.com/v1/me/following?type=artist&limit=50}}

Fetching done!
Done.