XMLHttpRequest on Flutter Dio Package with Spring Boot server - flutter

I'm trying to make a request to my server with a Flutter Web Application using Dio package. It's returning the XMLHttpRequest error but the server is receveing the request normally.
I'm aware that with disable security command the problem vanish, but I need a solution for production as weel.
Dio code:
import 'package:dio/dio.dart';
import '../interceptor/api_interceptor.dart';
class DioApi {
static String backendUrl = "http://localhost:8080/api/";
static Dio _getDio({Map<String, dynamic>? headers}) {
BaseOptions baseOptions = BaseOptions(
baseUrl: backendUrl,
receiveDataWhenStatusError: true,
connectTimeout: 60 * 1000,
receiveTimeout: 60 * 1000,
);
if (headers != null) {
baseOptions.headers.addAll(headers);
}
Dio dio = Dio(baseOptions);
dio.interceptors.add(ApiInterceptor());
return dio;
}
static Future<Response> get(
String url,
{Map<String, dynamic>? headers, Map<String, dynamic>? queryParameters}) async {
return _getDio(headers: headers).get(url, queryParameters: queryParameters);
}
static Future<Response> post(
String url,
dynamic data,
{Map<String, dynamic>? headers, Map<String, dynamic>? queryParameters}) async {
return _getDio(headers: headers).post(url, data: data, queryParameters: queryParameters);
}
}
--disable-web-security can't help me and CORS on server side seems fine.

Related

How to set timeout for Api call using retrofit in flutter?

Where we can set timeout in below code?
As you can see I am using retrofit for api call.
Dio Object
class DioObject{
static Dio getDio(){
debugPrint("Bearer:- ${PrefHelper().pref?.getString(PrefHelper.AUTHORIZATION)}");
final dio = Dio(); // Provide a dio instance
dio.options.headers["Authorization"] =
"Bearer ${PrefHelper().pref?.getString(PrefHelper.AUTHORIZATION)}"; // config your dio headers globally
dio.options.headers["Content-Type"] =
"application/json;charset=UTF-8"; // config your dio headers globally
return dio;
}
}
Api call
final client = RestClient(DioObject.getDio());
var response = await client.xyz();
Rest API
#RestApi(baseUrl: "*****/api")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
#GET("/UserAccessPoints/")
Future<CommonResponse> xyz();
}
You can do it the same way you added your custom headers, or doing it all at once with a BaseOptions object
final dio = Dio();
//Dio Options
dio.options = BaseOptions(
contentType: 'application/json',
connectTimeout: 4000,
sendTimeout: 4000,
receiveTimeout: 10000,
headers : ...
);

How to fire interceptor with post request and set configurable dio interceptor work every api app

I want to know dio post request how perform interceptor 'onrequest' in response 'onerror'
Above code in 'onerror' request refresh token not working interceptor also any other way
Future getlist() async{
final dio = Dio();
Final storage=Fluttersecurestorage();
String accesstoken =await storage.read('accesstoken');
final response= await dio.post(
url,
data: loginData,
options: Options(headers: {"authorization": 'Bearer $accesstoken'}),
dio.interceptors.clear();
dio.interceptors.add(
InterceptorsWrapper(
onRequest: (request, handler) {
},
onError: (err, handler) async {
if (err.response?.statusCode == 401) {
//request refresh token}
}
}
));

Flutter Test for API Helper class using Dio

I am using a helper class to make request to server for that I am using Dio.
I want to test the helper class
this is my helper class
import 'package:dio/dio.dart';
import '../error/exceptions.dart';
enum HttpMethod { GET, POST, PUT, DELETE }
abstract class ApiClient {
Future<Response> request(
HttpMethod method,
String path, {
Map<String, dynamic>? headers,
Map<String, dynamic>? queryParams,
dynamic body,
});
}
/// This class provides http calls using dio package
class ApiClientImpl implements ApiClient {
final Dio dio;
ApiClientImpl({required this.dio});
#override
Future<Response> request(
HttpMethod method,
String path, {
Map<String, dynamic>? headers,
Map<String, dynamic>? queryParams,
dynamic body,
}) async {
// we can add headers here which are common for every restapi call
// headers = {'content-type': 'application/json'};
try {
final _response = await dio.request(
path,
options: Options(
method: _getApiMethodString(method),
headers: headers,
),
queryParameters: queryParams,
data: body,
);
return _response;
} on DioError {
throw FetchDataException('Dio Error Occurred');
}
}
String _getApiMethodString(HttpMethod method) {
switch (method) {
case HttpMethod.GET:
return 'GET';
case HttpMethod.POST:
return 'POST';
case HttpMethod.PUT:
return 'PUT';
case HttpMethod.DELETE:
return 'DELETE';
}
}
}
The test I wrote for it is
#GenerateMocks([Dio])
void main() {
late MockDio dio;
late ApiClientImpl apiClient;
setUp(() {
dio = MockDio();
apiClient = ApiClientImpl(dio: dio);
});
group('ApiService class methods test', () {
test('Should return response when request to server is made', () async {
// arrange
var successMessage = {'message': 'Success'};
const baseUrl = 'https://example.com/';
final options = Options(method: 'GET', headers: null);
when(dio.request(baseUrl,
options: options, queryParameters: anyNamed('queryParameters')))
.thenAnswer((_) async => Response(
requestOptions: RequestOptions(path: baseUrl),
data: successMessage,
statusCode: 200));
// act
final response = await apiClient.request(HttpMethod.GET, baseUrl);
// assert
expect(response.data, successMessage);
});
});
}
I am getting an error
MissingStubError: 'request'
No stub was found which matches the arguments of this method call:
request('https://example.com/', {data: null, queryParameters: null, cancelToken: null, options: Instance of 'Options', onSendProgress: null, onReceiveProgress: null})
How Can I write a correct test for the helper class.

Could not set header with dio

I ma not able to set header with dio.I am tryng to set my access token to the header.I ma trying to set header so that every request doesnt required to call it.Here is my network class where i am trying to call header with dio
My network Class:
class NetworkUtil {
Dio _dio;
String token;
getToken() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
String getToken = preferences.getString(AppPrefernces.LOGIN_PREF);
return getToken;
}
NetworkUtil() {
///Create Dio Object using baseOptions set receiveTimeout,connectTimeout
BaseOptions options = BaseOptions(receiveTimeout: 5000, connectTimeout: 5000);
options.baseUrl = ApiConstants.BASE_URL;
_dio = Dio(options);
_dio.interceptors.add(InterceptorsWrapper(
onRequest: (Options option) async{
//my function to recovery token
await getToken().then((result) {
LoginResponse loginResponse = LoginResponse.fromJson(jsonDecode(result));
token = loginResponse.accessToken;
});
option.headers = {
"Authorization": "Bearer $token"
};
}
));
}
///used for calling Get Request
Future<Response> get(String url, Map<String, String> params) async {
Response response = await _dio.get(url,
queryParameters: params,
options: Options(responseType: ResponseType.json));
return response;
}
///used for calling post Request
Future<Response> post(String url, Map<String, String> params) async {
Response response = await _dio.post(url,
data: params, options: Options(responseType: ResponseType.json));
return response;
}
}
I use this setup and it works fine for me.
Future<Dio> createDioWithHeader() async {
if (_dioWithHeader != null) return _dioWithHeader;
String token = await appSharedPreferences.getToken();
String userAgent = await getUserAgent();
print('User-Agent: $userAgent');
// base config
_dioWithHeader = Dio(BaseOptions(
connectTimeout: 10000,
receiveTimeout: 10000,
baseUrl: Config.apiBaseUrl,
contentType: 'application/json',
headers: {
'Authorization': token,
'User-Agent': userAgent
}));
// setup interceptors
return addInterceptors(_dioWithHeader);
}```

Global configuration (interceptor) for dio in Flutter

First time with Flutter. I'm using dio to send HTTP requests, and I have to add a header to all requests, which I do with an interceptor, like this:
Dio dio = new Dio();
dio.interceptors.add(InterceptorsWrapper(
onRequest:(RequestOptions options) async {
options.headers["X-Requested-With"] = "XMLHttpRequest";
})
);
It works in main.dart, but if I want to import another class like MyHomePage.dart and do HTTP requests there, I'd have to redefine the interceptor in that class too.
How can I implement this interceptor for my whole application without adding it in every .dart file?
Create a function that houses the DIO and then call it where needed
Dio getDio() {
Dio dio = new Dio();
dio.interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
options.headers["X-Requested-With"] = "XMLHttpRequest";
}));
return dio;
}
This worked good for me, without interceptors, just create a class and use it in your app.
import 'package:dio/dio.dart';
import '../helpers/api_url.dart';
class dioClient {
Dio dio = Dio();
static Dio simpleDio() {
return Dio(BaseOptions(
baseUrl: apiUrl(),
headers: {'Content-Type': 'application/json; charset=UTF-8'}));
}
static Dio dioWithCookie(String cookie) {
return Dio(BaseOptions(baseUrl: apiUrl(), headers: {
'Content-Type': 'application/json; charset=UTF-8',
'Cookie': cookie
}));
}
}