How to add headers to dio http request in Flutter

I have this put request that sends a file to the server, it requires a token as part of the headers. Please how do I add this token to the header while using Dio().put to make my request?

 Future<String> sendImage(File file) async {
    String token = storage.read('token');
    String fileName = file.path.split('/').last;
    FormData formData = FormData.fromMap({
      "CustomerProfilePicture":
          await MultipartFile.fromFile(file.path, filename: fileName),
    });
    Response response =
        await Dio().put(
            '$BASE_URL/Customers/Picture', data: formData);
    return response.statusMessage;
  }

Upvotes: 2

Views: 7931

Answers (2)

Gourango Sutradhar
Gourango Sutradhar

Reputation: 1619

This is the way to put header to your DIO request:

await Dio().put('$BASE_URL/Customers/Picture',
    data: formData,
    options: Options(
      headers: {"key": "value"},
    ));

Upvotes: 4

mkobuolys
mkobuolys

Reputation: 5333

You can pass headers by using the options property for your request:

await Dio().put(
  '$BASE_URL/Customers/Picture',
  data: formData,
  options: Options(
    headers: {
      ...
    }
  ),
);

For more info, check the documentation.

Upvotes: 1

Related Questions