Home > front end >  Calling http get for url which contains data with spaces
Calling http get for url which contains data with spaces

Time:10-05

When I make a call to this function , it works if the artist and album has data without spaces. How would I change this to make it consider data with spaces .

  static Future fetchAlbumDetailData(String albumName, String artist) async {
    print('ALBUM NAME : $albumName');
    print('ALBUM artist : $artist');
    print(
        'Link called is :https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=$apiKey&artist=$artist&album=$albumName&format=json');
    http.Response response = await retry(
      // Make a GET request
      () => http
          .get(
            Uri.parse(
                'https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=$apiKey&artist=$artist&album=$albumName&format=json'),
             )
          .timeout(const Duration(seconds: 5)),
      // Retry on SocketException or TimeoutException
      retryIf: (e) => e is SocketException || e is TimeoutException,
    );

    if (response.statusCode == 200) {
      return response.body;
    } else {
      throw (response.statusCode);
    }
  }

My print statement output is :

I/flutter ( 5341): ALBUM NAME : Make Believe I/flutter ( 5341): ALBUM artist : Weezer I/flutter ( 5341): Link called is :https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=755bf5e882b716dac814852b5e8e2e52&artist=Weezer&album=Make Believe&format=json

The link is destroyed between the words Make and Believe

CodePudding user response:

Please replace space with before making get call. like this. albumName.replaceAll(" "," ")

CodePudding user response:

You don't actually need to replace the String as the Uri.parse turns your space into for you.

Here I test this simple app and seems the call is working fine.

As for your null check operator error, it usually happens with the exclamation notation! when you use on a value to force it to be not-null, but it actually is null. It would help if you share more of your code (null-safe related parts) so we can support further.

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final uri =
      'https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=755bf5e882b716dac814852b5e8e2e52&artist=Weezer&album=Make Believe&format=json';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sample App'),
        ),
        body: Container(
            child: Center(
          child: TextButton(
            child: Text('Press me'),
            onPressed: () => fetchAlbumDetailData(uri),
          ),
        )),
      ),
    );
  }

  static Future fetchAlbumDetailData(_uri) async {
    print(Uri.parse(_uri));
    // Print out: https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=755bf5e882b716dac814852b5e8e2e52&artist=Weezer&album=Make Believe&format=json
    final result = await http
        .get(Uri.parse(_uri))
        .timeout(const Duration(seconds: 5));

    print(result.body);
  }
}

CodePudding user response:

The Uri class provides encodeFull, encodeComponent, and encodeQueryComponent methods to escape characters that are reserved for URIs for other purposes.

In your case, since your string will be part of a query string, you should use Uri.encodeQueryComponent. Note that this will encode spaces as and not as , but is more correct for query string components. (Also see: URL encoding the space character: or ?)

  • Related