syed shah
syed shah

Reputation: 91

Flutter Dart HTTP headers not working. Sending Request with "headers". On response authorization error, {"CODE":401,"MESSAGE":"Unauthorized"}

Sending request to API with authorization and other headers, its returning unauthorized. it seems like server is not getting headers while requesting. i tried different approaches to send http headers on request but failed. I tried Dio , HttpClient, Normal http request all are failed. I spent my 2 days on this thing, still not resolved. from Postman, and other mediums request is working.

Map<String,String> reqHeaders = {
  'Content-type': 'application/json',
  'Accept': 'application/json',
  "Authorization": "xxxxx",
  "langapi": "en"
};
Future<MzResponseData> getHttp() async {
  var dio = Dio();
  dio.options.baseUrl = baseUrl;
  dio.options.headers = reqHeaders;
  dio.options.contentType = ContentType.parse("application/json");
  Response response = await dio.get("/uri/");
  print(response);
}

Upvotes: 0

Views: 4224

Answers (2)

Tayseer Dalati
Tayseer Dalati

Reputation: 308

Try this..

final response = await dio.get(
      url,
      options: Options(
        headers: {
          'Authorization': 'Bearer $token',
        },
      )
    );

Upvotes: 4

user9139407
user9139407

Reputation: 1022

Future<MzResponseData> getHttp() async {
  var dio = await Dio();
  dio.options.baseUrl = baseUrl;
    dio.options.headers = Options(headers: {'Authorization': 'Bearer $token'})//add your type of authentication
  dio.options.contentType = ContentType.parse("application/json");
  Response response = await dio.get("/uri/");
  print(response);
}

Upvotes: 0

Related Questions