40

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

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
Vexal
  • 695
  • 2
  • 9
  • 16
  • 1
    You are printing the response headers, which look basically correct for response headers. What are you trying to print? – Richard Heap Nov 07 '19 at 15:43
  • @RichardHeap I try to add authorization key into header, but when i send to the server, The server can not get the header of authorization. – Vexal Nov 08 '19 at 03:05
  • Check that your server accepts the header name `authorization` in lower case. Dart will typically lower case all header name. Use Postman to confirm that a lower case header name works. – Richard Heap Nov 08 '19 at 12:42
  • Just found the problem with server side checking header. It's not work with `apache_request_headers()` on **Laravel**, need to use `req->request();`. – Vexal Nov 14 '19 at 03:19
  • It's probably best to delete this question, as the solution is not actually related to the question. Your Dart code turned out to be correct. – Richard Heap Nov 14 '19 at 09:53

7 Answers7

43

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.

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
17

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>",
            },
          ),
        );
Monasha
  • 882
  • 1
  • 12
  • 17
Jashan PJ
  • 4,177
  • 4
  • 27
  • 41
16

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);
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
2

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";
1

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

BartusZak
  • 1,041
  • 14
  • 21
0
dio.options.contentType = ContentType("application","x-www-form-urlencoded") as String;
Mol0ko
  • 2,938
  • 1
  • 18
  • 45
0

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;
};
Hadi Note
  • 1,386
  • 17
  • 16