Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 460 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to add header for post method in dio in Flutter

#1
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
Reply

#2
There are some similar questions do not have answer <br>
But the following work for me <br>
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);
Reply

#3
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.
Reply

#4
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";

Reply

#5
That's probably bug because I was not able to set it with lowercase `content-type`.

`Content-Type` works.

```dart
options.headers = {'Content-Type': 'application/json', ...request.headers};
```

Feel free to refer to

[To see links please register here]

Reply

#6
```dart
dio.options.contentType = ContentType("application","x-www-form-urlencoded") as String;
```
Reply

#7
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>",
},
),
);
Reply

#8
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;
};
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through