0

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);
}
8
  • try with Postman with the same authentication. I think the error is coming also on postman Commented Jun 20, 2019 at 7:56
  • I tried to send request with authentication headers on postman also, there its working fine. but on dart its giving error of authentication. it seems like header is not attaching while requesting on dart . Commented Jun 20, 2019 at 8:01
  • okay. what type of authentication? Commented Jun 20, 2019 at 8:04
  • Authorization:xxxxx Commented Jun 20, 2019 at 8:06
  • 2
    @syedshah Have you solve this? Please how did you solve it, Am having the same problem, Have searched and I don't see any solution Commented Jan 23, 2020 at 13:34

2 Answers 2

4

Try this..

final response = await dio.get(
      url,
      options: Options(
        headers: {
          'Authorization': 'Bearer $token',
        },
      )
    );
Sign up to request clarification or add additional context in comments.

Comments

0
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);
}

2 Comments

i tried your code but its giving error on line"Options(headers: {'Authorization'".ERROR: A value of type 'Options' can't be assigned to a variable of type 'Map<String, dynamic>
dio.options.headers.addAll(reqHeaders); i tried this... but still not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.