Google book api in postman working fine But in flutter giving status code 404

Viewed 233

When I run google book API in postman It is working fine and Giving status code 200. But In flutter, it gives the following error. And Giving 404 status code. I have generated API Key and also I have enabled Google Book API. So please help me If anyone knows what I'm doing wrong.

TestLayout.dart

   class TestLayout extends StatefulWidget {
      static String id = "Test_Layout";
    
      @override
      _TestLayoutState createState() => _TestLayoutState();
    }
    
    class _TestLayoutState extends State<TestLayout> {
      Future<void> getData() async {
        String ApiKey = "..........";
    
        final response = await http.get(
            Uri.https("books.googleapis.com",
                "books/v1/volumes?q=flowers+inauthor:keyes&key=$ApiKey"),
            headers: {"Content-Type": "application/json"});
    
        var jsonBody = jsonDecode(response.body);
    
        //book b1=book.fromJson(jsonBody);
    
        print(response.statusCode);
        print(b1.id);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Flutter"),
          ),
          body: Column(
            children: [
              TextButton(
                  onPressed: () async {
                    await getData();
                  },
                  child: Text("Submit")),
            ],
          ),
        );
      }
    }
   

Book.dart:

class book {

  final int id;

   book({this.id});

   factory book.fromJson(Map<String,dynamic> json)
   {
     return book(
       id: json["totalItems"]
     );
   }
}
   

Error:

    E/flutter (27854): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: 
    FormatException: Unexpected character (at character 1)
    E/flutter (27854): <!DOCTYPE html>
    E/flutter (27854): ^
    E/flutter (27854): 
    E/flutter (27854): #0      _ChunkedJsonParser.fail (dart:convert- 
    patch/convert_patch.dart:1404:5)
    E/flutter (27854): #1      _ChunkedJsonParser.parseNumber (dart:convert- 
     patch/convert_patch.dart:1271:9)
    E/flutter (27854): #2      _ChunkedJsonParser.parse (dart:convert- 
    patch/convert_patch.dart:936:22)
    E/flutter (27854): #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
    E/flutter (27854): #4      JsonDecoder.convert (dart:convert/json.dart:506:36)
    E/flutter (27854): #5      JsonCodec.decode (dart:convert/json.dart:157:41)
    E/flutter (27854): #6      jsonDecode (dart:convert/json.dart:96:10)
    E/flutter (27854): #7      _TestLayoutState.getData 
    (package:freebookshare/TestFile/TestLayout.dart:22:20)
    E/flutter (27854): <asynchronous suspension>
    E/flutter (27854): #8      _TestLayoutState.build.<anonymous closure> 
    (package:freebookshare/TestFile/TestLayout.dart:40:17)
    E/flutter (27854): <asynchronous suspension>
    E/flutter (27854): 
2 Answers

I Used Uri.parse() method and it worked.

String url="https://books.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=$ApiKey";

final response = await http.get(Uri.parse(url),
    headers: {"Content-Type": "application/json"});

This was an old issue that I also had previously. After digging some research, I got my necessary output by removing the Uri Constructor in my API call. Try to remove Uri Constructor and add the code for the final response as follows

    final response = await http.get("https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=$apiKey",
        headers: {"Content-Type": "application/json"});

After this, the code of TestLayout will be as follows

    class TestLayout extends StatefulWidget {
  static String id = "Test_Layout";

  @override
  _TestLayoutState createState() => _TestLayoutState();
}

class _TestLayoutState extends State<TestLayout> {
  Future<void> getData() async {
    String apiKey = "..........";

    final response = await http.get("https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=$apiKey",
        headers: {"Content-Type": "application/json"});

    var jsonBody = jsonDecode(response.body);
    
    book b1=book.fromJson(jsonBody);
    
    print(response.statusCode);
    print(b1.id);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter"),
      ),
      body: Column(
        children: [
          TextButton(
              onPressed: () async {
                await getData();
              },
              child: Text("Submit")),
        ],
      ),
    );
  }
}

Hope this will work for you

Related