Q: how can i call get API using headers in flutter? any idea? i have auth_key for fetch list.
i tried many times but auth_key did not get in second screen, due to i can not fetch data from internet
I tested the API with JSON request headers in Postman and it seems to be working fine.
this is the Response...
{
"error": false,
"message": "success",
"course_list": [
{
"courseId": "19",
"courseName": "Verbs-01-def-prctice",
"price": "0",
"sub_type": "1",
"sku_id": "15464",
"courseIcon": "",
"type": "0",
"paid": false
},
    **Now on my Flutter application I am trying to do the same thing:  
**
    
        
    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:http/http.dart' as http;
    import 'dart:convert';
    
    import 'package:http/http.dart';
    
    class SecondScreen extends StatefulWidget {
      @override
      _SecondScreenState createState() => _SecondScreenState();
    }
    
    class _SecondScreenState extends State<SecondScreen> {
      String url = '';
      List data;
    
      @override
      void initState() {
        super.initState();
        this.getJsonData();//call method here
      }
    
      Future<http.Request> getJsonData() async {
        var response = await http
            .get(url, headers: {'auth_key': '7tJBAO3xwIF3UhfIwMYryL2Cqo1dpQ'});//this is my auth_key(token)
    
        print(response.body);
    
        setState(() {
          var convertDataToJson = json.decode(response.body);
          data = convertDataToJson['results'];
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Courses"),
          ),
          body: new ListView.builder(
            itemCount: data == null ? 0 : data.length,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                child: Center(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Card(
                        child: Container(
                          child: Text(data[index]['courseName']),
                          padding: EdgeInsets.all(20.0),
                        ),
                      )
                    ],
                  ),
                ),
              );
            },
          ),
        );
      }
    }

after run this App...
Xcode build done.                                           14.3s
Installing and launching...
Syncing files to device Samcom Tech's iPhone...
flutter:  {"error":true,"message":"Sorry, Auth key is not defined"}
[enter image description here][1]

A: Normally you would need to use "Authorization" instead of "auth_key" in your header. Please try that. Or, please try the following example:
Map<String, String> headers = {
  HttpHeaders.authorizationHeader: "7tJBAO3xwIF3UhfIwMYryL2Cqo1dpQ",
  HttpHeaders.contentTypeHeader: "application/json"
};
var response = await http
        .get(url, headers: headers);


A: If you want there is a great library to to your http requests called CHOPPER:
https://pub.dev/packages/chopper
You can also find a great tutorial on: 
https://www.youtube.com/watch?v=zFXK5EsrUF0&t=1553s
Here's an code example:
// my_service.dart

import "dart:async";
import 'package:chopper/chopper.dart';

part "my_service.chopper.dart";

@ChopperApi(baseUrl: "/resources")
abstract class MyService extends ChopperService {
  static MyService create([ChopperClient client]) => _$MyService(client);

  @Get(url: "{id}")
  Future<Response> getResource(@Path() String id);

  @Get(headers: const {"foo": "bar"})
  Future<Response<Map>> getMapResource(@Query() String id);

  @Post(url: 'multi')
  @multipart
  Future<Response> postResources(
    @Part('1') Map a,
    @Part('2') Map b,
    @Part('3') String c,
  );

  @Post(url: 'file')
  @multipart
  Future<Response> postFile(
    @FileField('file') List<int> bytes,
  );
}


A: Please try below code:
response = http.get(URL, headers: {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
        "auth_key': '7tJBAO3xwIF3UhfIwMYryL2Cqo1dpQ"
      }).then((http.Response response) {});

