Reputation: 91
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
Reputation: 308
Try this..
final response = await dio.get(
url,
options: Options(
headers: {
'Authorization': 'Bearer $token',
},
)
);
Upvotes: 4
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