Reputation: 804
I'm using dio: ^3.0.4. Any one please help me to find the solution for adding header. here my code:
FormData formData =
new FormData.fromMap({"files": await MultipartFile.fromFile(filePath.path, filename: 'photo')
});
Response response = await dio.post("***********",
data: formData,
onSendProgress: (int sent, int total) {
print("$sent $total");
},
options: Options(
headers: {
"authorization": "*************"
},
followRedirects: false,
validateStatus: (status) {
return status <= 500;
}
),
);
When i print the header.
print(response.headers);
Result:
flutter: content-type: text/html; charset=UTF-8 connection: close cache-control: no-cache, private transfer-encoding: chunked date: Thu, 07 Nov 2019 14:29:02 GMT server: Apache/2.4.18
Upvotes: 50
Views: 85902
Reputation: 1414
I'm using it like this:
_dio = Dio();
_dio.options.baseUrl = _baseUrl;
_dio.options.connectTimeout = 5000; //5s
_dio.options.receiveTimeout = 3000;
_dio.options.contentType = 'application/json';
_dio.options.headers['Content-Type'] = 'application/json';
_dio.options.headers["Authorization"] = "Bearer 284|NRx1PaEY2HbwHi2XMzGdxg9UJ5rGXkNMcYyNXkqH";
_dio.options.followRedirects = false;
_dio.options.validateStatus = (status) {
return status! < 500;
};
Upvotes: 0
Reputation: 4398
In case of you use di in your projects and the dioclient is a singleton, this is how authorization is added to the call.
final response = await _dioClient.get(Endpoints.getDashboard,
queryParameters{'shopId':int.parse(shopId)},
options: Options(
headers: {
"authorization": "Bearer <your token>",
},
),
);
Upvotes: 22
Reputation: 1
dio.options.contentType = ContentType("application","x-www-form-urlencoded") as String;
Upvotes: 0
Reputation: 1253
That's probably bug because I was not able to set it with lowercase content-type
.
Content-Type
works.
options.headers = {'Content-Type': 'application/json', ...request.headers};
Feel free to refer to https://github.com/flutterchina/dio/issues/1045
Upvotes: 1
Reputation: 114
This work for me after try differents ways to pass the argument to headers
Dio dio = new Dio();
dio.options.contentType = ContentType("application","x-www-form-urlencoded");
dio.options.headers[HttpHeaders.authorizationHeader] ="Basic $clientCredentials";
Upvotes: 2
Reputation: 4728
Dio library key working perfectly fine in my case if we pass small case key value
For example,
Dio dio = new Dio();
dio.options.headers['content-Type'] = 'application/json';
dio.options.headers["authorization"] = "token ${token}";
response = await dio.post(url, data: data);
make sure you write key in small case, that's work for me.
Upvotes: 50
Reputation: 54367
There are some similar questions do not have answer
But the following work for me
Please use the following code snippet to set headers attribute
Dio dio = new Dio();
dio.options.headers["Authorization"] = "Bearer ${token}";
response = await dio.post(url, data: data);
Upvotes: 17