Validate localhost API request processed by POSTMAN using Regular Expression
Given some Postman APIs, the task is to check if they are valid or not using regular expressions.
Rules for the valid API:
- It is an alphanumeric String, a combination of alphabets ( ‘a’ to ‘z’) and digits (0 to 9).
- It should always start with HTTP and must contain some special symbols (‘:’, ‘/’)
- It should not contain any whitespaces.
Examples:
Input: http://localhost:1337/api/products
Output: TrueInput: http://localhost:30000/user/kyc
Output: True
Approach: The problem can be solved using a regular expression based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\, \’\\/\\\\\\+&%\\$#_]*)?$“.Where,
^ : This indicates the start of the string
? : Represents either that character should be present or not.
[0-9a-zA-Z] : Any character either from digit or alphabet can be included.
$ :End of the string.
Follow the below steps to implement the idea:
- Create a regex expression for Postman API
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the API is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above approach:-
C++
// C++ program to validate the // localhost API request processed by POSTMAN // using Regular Expression #include <bits/stdc++.h> #include <regex> using namespace std; // Function to validate the // localhost API request processed by POSTMAN string isValidLocalhost_APIRequest(string str) { // Regex to check valid // MICR Code. const regex pattern( "^(ht|f)tp(s?)\\:\\/\\/" "[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/" "?)([a-zA-Z0-9\\-\\.\\?\\, \'\\/" "\\\\\\+&%\\$#_]*)?$" ); // If the str // is empty return false if (str.empty()) { return "false" ; } // Return true if the str // matched the ReGex if (regex_match(str, pattern)) { return "true" ; } else { return "false" ; } } // Driver Code int main() { // Test Case 1: cout << isValidLocalhost_APIRequest(str1) << endl; // Test Case 2: cout << isValidLocalhost_APIRequest(str2) << endl; // Test Case 3: cout << isValidLocalhost_APIRequest(str3) << endl; // Test Case 4: string str4 = "www.gmail.com" ; cout << isValidLocalhost_APIRequest(str4) << endl; // Test Case 5: cout << isValidLocalhost_APIRequest(str5) << endl; // Test Case 6: string str6 = "localhost:8000/students" ; cout << isValidLocalhost_APIRequest(str6) << endl; return 0; } |
Java
// Java program to validate the // localhost API request processed by POSTMAN // using Regular Expression import java.util.regex.*; class GFG { // Function to validate the // localhost API request processed by // POSTMAN public static boolean isValidLocalhost_APIRequest(String str) { // Regex to check valid localhost API // request String regex = "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]" + "*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\" + "-\\.\\?\\, \'\\/\\\\\\+&%\\$#_]*)?$" ; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the str // is empty return false if (str == null ) { return false ; } // Pattern class contains matcher() // method to find matching between // given Postman API request using regex. Matcher m = p.matcher(str); // Return if the str // matched the ReGex return m.matches(); } // Driver code public static void main(String args[]) { // Test Case 1: String str1 = "http:/" + "/localhost:8082/api/secure" ; System.out.println( isValidLocalhost_APIRequest(str1)); // Test Case 2: String str2 = "http:/" + "/localhost:1337/api/products" ; System.out.println( isValidLocalhost_APIRequest(str2)); // Test Case 3: String str3 = "https:/" + "/shiptos.com:8081/booking" ; System.out.println( isValidLocalhost_APIRequest(str3)); // Test Case 4: String str4 = "www.gmail.com" ; System.out.println( isValidLocalhost_APIRequest(str4)); // Test Case 5: String str5 = "http:/" + "/localhost:30000/sends/search" ; System.out.println( isValidLocalhost_APIRequest(str5)); // Test Case 6: String str6 = "localhost:8000/students" ; System.out.println( isValidLocalhost_APIRequest(str6)); } } |
Python3
# Python3 program to validate # localhost API request processed by POSTMAN # using Regular Expression import re # Function to validate # localhost API request processed by POSTMAN def isValidLocalhost_APIRequest( str ): # Regex to check valid localhost API request # processed by POSTMAN regex = "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]" \ "*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\" \ "?\\, \'\\/\\\\\\+&%\\$#_]*)?$" # Compile the ReGex p = re. compile (regex) # If the string is empty # return false if ( str = = None ): return "false" # Return if the string # matched the ReGex if (re.search(p, str )): return "true" else : return "false" # Driver code if __name__ = = '__main__' : # Test Case 1: print (isValidLocalhost_APIRequest(str1)) # Test Case 2: print (isValidLocalhost_APIRequest(str2)) # Test Case 3: print (isValidLocalhost_APIRequest(str3)) # Test Case 4: str4 = "www.gmail.com" print (isValidLocalhost_APIRequest(str4)) # Test Case 5: print (isValidLocalhost_APIRequest(str5)) # Test Case 6: str6 = "localhost:8000 / students" print (isValidLocalhost_APIRequest(str6)) |
C#
// C# program to validate the // localhost API request processed by POSTMAN // using Regular Expression using System; using System.Text.RegularExpressions; public class GFG{ // Function to validate the // LEI Code public static bool isValidLocalhost_APIRequest(String str) { // Regex to check valid localhost API // request var regex = new Regex( "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]" + "*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\" + "-\\.\\?\\, \'\\/\\\\\\+&%\\$#_]*)?$" ); // If the str // is empty return false if (str == null ) { return false ; } // Pattern class contains matcher() // method to find matching between // given Postman API request using regex. var m = regex.Match(str); // Return if the str // matched the ReGex return m.Success; } // Driver Code. public static void Main(String[] args) { // Test Case 1: var str1 = "http:/" + "/localhost:8082/api/secure" ; Console.WriteLine(GFG.isValidLocalhost_APIRequest(str1)); // Test Case 2: var str2 = "http:/" + "/localhost:1337/api/products" ; Console.WriteLine(GFG.isValidLocalhost_APIRequest(str2)); // Test Case 3: var str3 = "https:/" + "/shiptos.com:8081/booking" ; Console.WriteLine(GFG.isValidLocalhost_APIRequest(str3)); // Test Case 4: var str4 = "www.gmail.com" ; Console.WriteLine(GFG.isValidLocalhost_APIRequest(str4)); // Test Case 5: var str5 = "http:/" + "/localhost:30000/sends/search" ; Console.WriteLine(GFG.isValidLocalhost_APIRequest(str5)); // Test Case 6: var str6 = "localhost:8000/students" ; Console.WriteLine(GFG.isValidLocalhost_APIRequest(str6)); } } // This code is contributed by Potta Lokesh |
Javascript
// Javascript program to validate the // localhost API request processed by POSTMAN // using Regular Expression // Function to validate the // localhost API request processed by POSTMAN function isValidLocalhost_APIRequest(str) { // Regex to check valid // MICR Code. let regex = new RegExp( "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\, \’\\/\\\\\\+&%\\$#_]*)?$" ); // If the str // is empty return false if (str == null ) { return "false" ; } // Return true if the str // matched the ReGex if (regex.test(str) == true ) { return "true" ; } else { return "false" ; } } // Driver Code // Test Case 1: console.log(isValidLocalhost_APIRequest(str1)+ "<br>" ); // Test Case 2: console.log(isValidLocalhost_APIRequest(str2)+ "<br>" ); // Test Case 3: console.log(isValidLocalhost_APIRequest(str3)+ "<br>" ); // Test Case 4: let str4 = "www.gmail.com" ; console.log(isValidLocalhost_APIRequest(str4)+ "<br>" ); // Test Case 5: console.log(isValidLocalhost_APIRequest(str5)+ "<br>" ); // Test Case 6: let str6 = "localhost:8000/students" ; console.log(isValidLocalhost_APIRequest(str6)+ "<br>" ); // This code is contributed by Utkarsh |
true true true false true false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...