apidash icon indicating copy to clipboard operation
apidash copied to clipboard

Add ability to send https requests without SSL verification

Open pacnpal opened this issue 2 years ago • 13 comments

Tell us about the task you want to perform and are unable to do so because the feature is not available

Unable to send https requests to servers that don't have certificates, such as local IP addresses

Describe the solution/feature you'd like us to add

Add function for curl request to be sent with the -k option, ignoring the certificate verification step

pacnpal avatar Oct 29 '24 13:10 pacnpal

Thank you for raising the issue. This is definitely a useful feature. Also, a good issue on which people can contribute.

ashitaprasad avatar Oct 30 '24 20:10 ashitaprasad

can you assign me this issue?

nilaygit-10721 avatar Oct 31 '24 12:10 nilaygit-10721

@nilaygit-10721 We do not assign any issues. Anyone can work on the issue and submit a PR.

ashitaprasad avatar Oct 31 '24 12:10 ashitaprasad

@ashitaprasad i would love to work on this since i myself faced this issue while working on adding codgen for hyper. My approach and changes would be: -> adding a toogle button in settings for ssl certificate verification (by default its turned on.) -> changing settings model and adding a provider for ssl. -> make a custom http client that can make requests without checking for ssl certificates and use that client when ssl certification is turned off.

Clasherzz avatar Nov 12 '24 07:11 Clasherzz

@Clasherzz Can you share how the custom client will look in this thread.

ashitaprasad avatar Nov 14 '24 08:11 ashitaprasad

@Clasherzz Can you share how the custom client will look in this thread.

Sure thing @ashitaprasad i have tested this client on https://expired.badssl.com/ and received the html i saw on the site as response body


import 'package:http/http.dart' as http;

import 'package:http/io_client.dart';

import 'dart:io';



/// Create a custom `HttpClient` with SSL verification disabled.
http.Client createHttpClientWithNoSSL() {
  var ioClient = HttpClient()
    ..badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
  return IOClient(ioClient);
}

// Use this client in your function
// Future<(http.Response?, Duration?, String?)> request(
//   HttpRequestModel requestModel, {
//   String defaultUriScheme = kDefaultUriScheme,
// }) async {
//   (Uri?, String?) uriRec = getValidRequestUri(
//     requestModel.url,
//     requestModel.enabledParams,
//     defaultUriScheme: defaultUriScheme,
//   );

//   // Replace the default client with the custom one
//   http.Client client = createHttpClientWithNoSSL();

//   if (uriRec.$1 != null) {
//     Uri requestUrl = uriRec.$1!;
//     Map<String, String> headers = requestModel.enabledHeadersMap;
//     http.Response response;
//     String? body;

//     try {
//       Stopwatch stopwatch = Stopwatch()..start();
//       var isMultiPartRequest =
//           requestModel.bodyContentType == ContentType.formdata;

//       if (kMethodsWithBody.contains(requestModel.method)) {
//         var requestBody = requestModel.body;
//         if (requestBody != null && !isMultiPartRequest) {
//           var contentLength = utf8.encode(requestBody).length;
//           if (contentLength > 0) {
//             body = requestBody;
//             headers[HttpHeaders.contentLengthHeader] = contentLength.toString();
//             if (!requestModel.hasContentTypeHeader) {
//               headers[HttpHeaders.contentTypeHeader] =
//                   requestModel.bodyContentType.header;
//             }
//           }
//         }
//         if (isMultiPartRequest) {
//           var multiPartRequest = http.MultipartRequest(
//             requestModel.method.name.toUpperCase(),
//             requestUrl,
//           );
//           multiPartRequest.headers.addAll(headers);
//           for (var formData in requestModel.formDataList) {
//             if (formData.type == FormDataType.text) {
//               multiPartRequest.fields.addAll({formData.name: formData.value});
//             } else {
//               multiPartRequest.files.add(
//                 await http.MultipartFile.fromPath(
//                   formData.name,
//                   formData.value,
//                 ),
//               );
//             }
//           }
//           http.StreamedResponse multiPartResponse =
//               await multiPartRequest.send();
//           stopwatch.stop();
//           http.Response convertedMultiPartResponse =
//               await http.Response.fromStream(multiPartResponse);
//           return (convertedMultiPartResponse, stopwatch.elapsed, null);
//         }
//       }

//       switch (requestModel.method) {
//         case HTTPVerb.get:
//           response = await client.get(requestUrl, headers: headers);
//           break;
//         case HTTPVerb.head:
//           response = await client.head(requestUrl, headers: headers);
//           break;
//         case HTTPVerb.post:
//           response = await client.post(requestUrl, headers: headers, body: body);
//           break;
//         case HTTPVerb.put:
//           response = await client.put(requestUrl, headers: headers, body: body);
//           break;
//         case HTTPVerb.patch:
//           response = await client.patch(requestUrl, headers: headers, body: body);
//           break;
//         case HTTPVerb.delete:
//           response = await client.delete(requestUrl, headers: headers, body: body);
//           break;
     
        
//       }
//       stopwatch.stop();
//       return (response, stopwatch.elapsed, null);
//     } catch (e) {
//       return (null, null, e.toString());
//     } finally {
//       client.close();
//     }
//   } else {
//     return (null, null, uriRec.$2);
//   }
// }

void main() async {
 
  Uri uri = Uri.parse("https://expired.badssl.com/");
  

  http.Client client = createHttpClientWithNoSSL();

  try {

    final response = await client.get(uri);
    print(response.body);
  } catch (e) {
    print("Error: $e");
  } finally {
    client.close();  
  }
}

Clasherzz avatar Nov 14 '24 16:11 Clasherzz

@Clasherzz What about multipart request?

ashitaprasad avatar Nov 14 '24 19:11 ashitaprasad

@Clasherzz What about multipart request?

@ashitaprasad i would try parsing the multipart request made to the custom client

void main() async {
  Uri uri = Uri.parse("https://localhost:3443/upload");
  http.Client client = createHttpClientWithNoSSL();

  try {
    var request = http.MultipartRequest("POST", uri);

    request.fields['field1'] = 'value1';
    request.fields['field2'] = 'value2';


  
    var streamedResponse = await client.send(request);

   request.files.add(await http.MultipartFile.fromPath(
      'file',
      'C:/Users/HP/Downloads/DBMS',
    ));
    var response = await http.Response.fromStream(streamedResponse);

    print("Response status: ${response.statusCode}");
    print("Response body: ${response.body}");
  } catch (e) {
    print("Error: $e");
  } finally {
    client.close();
  }
}

this was tested on the upload endpoint in local system(code : https://github.com/Clasherzz/testing)

Clasherzz avatar Nov 16 '24 19:11 Clasherzz

@Clasherzz What about multipart request?

@ashitaprasad i would try parsing the multipart request made to the custom client

void main() async {
  Uri uri = Uri.parse("https://localhost:3443/upload");
  http.Client client = createHttpClientWithNoSSL();

  try {
    var request = http.MultipartRequest("POST", uri);

    request.fields['field1'] = 'value1';
    request.fields['field2'] = 'value2';


  
    var streamedResponse = await client.send(request);

   request.files.add(await http.MultipartFile.fromPath(
      'file',
      'C:/Users/HP/Downloads/DBMS',
    ));
    var response = await http.Response.fromStream(streamedResponse);

    print("Response status: ${response.statusCode}");
    print("Response body: ${response.body}");
  } catch (e) {
    print("Error: $e");
  } finally {
    client.close();
  }
}

this was tested on the upload endpoint in local system(code : https://github.com/Clasherzz/testing)

@ashitaprasad does this look good ?

Clasherzz avatar Nov 28 '24 10:11 Clasherzz

@Clasherzz Thanks for sharing the approach. Looks good, but we recently decided to put a hold to any http_service changes as we are planning to migrate to dio from http.

ashitaprasad avatar Dec 03 '24 19:12 ashitaprasad

@ashitaprasad i see. Is the organanization looking up for contributions for migrating to dio. Can i put up a issue and work on that ?

Clasherzz avatar Dec 04 '24 03:12 Clasherzz

@Clasherzz kindly take a look the Roadmap which now provides the project priorities and review timelines.

ashitaprasad avatar Dec 05 '24 17:12 ashitaprasad

Hi @pacnpal This feature is now available in API Dash. You can check out the doc - https://github.com/foss42/apidash/blob/main/doc/user_guide/disable_ssl.md to learn more about how to use this feature.

ashitaprasad avatar Dec 21 '24 12:12 ashitaprasad