Anish Hemachandran
Anish Hemachandran

Reputation: 79

Calling http get for url which contains data with spaces

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

Upvotes: 0

Views: 1837

Answers (3)

jamesdlin
jamesdlin

Reputation: 89926

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 %20, but + is more correct for query string components. (Also see: URL encoding the space character: + or %20?)

Upvotes: 1

Bach
Bach

Reputation: 3326

You don't actually need to replace the String as the Uri.parse turns your space into %20 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%20Believe&format=json
    final result = await http
        .get(Uri.parse(_uri))
        .timeout(const Duration(seconds: 5));

    print(result.body);
  }
}

Upvotes: 0

Mofidul Islam
Mofidul Islam

Reputation: 510

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

Upvotes: 0

Related Questions