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: True
Input: 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++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidLocalhost_APIRequest(string str)
{
const regex pattern(
"^(ht|f)tp(s?)\\:\\/\\/"
"[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/"
"?)([a-zA-Z0-9\\-\\.\\?\\, \'\\/"
"\\\\\\+&%\\$#_]*)?$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "http:// localhost:1337/api/products" ;
cout << isValidLocalhost_APIRequest(str1) << endl;
string str2 = "http:// localhost:1337/api/products" ;
cout << isValidLocalhost_APIRequest(str2) << endl;
string str3 = "https:// shiptos.com:8081/booking" ;
cout << isValidLocalhost_APIRequest(str3) << endl;
string str4 = "www.gmail.com" ;
cout << isValidLocalhost_APIRequest(str4) << endl;
string str5 = "http:// localhost:30000/sends/search" ;
cout << isValidLocalhost_APIRequest(str5) << endl;
string str6 = "localhost:8000/students" ;
cout << isValidLocalhost_APIRequest(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValidLocalhost_APIRequest(String str)
{
String regex
= "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]"
+ "*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\"
+ "-\\.\\?\\, \'\\/\\\\\\+&%\\$#_]*)?$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "http:/"
+ "/localhost:8082/api/secure" ;
System.out.println(
isValidLocalhost_APIRequest(str1));
String str2 = "http:/"
+ "/localhost:1337/api/products" ;
System.out.println(
isValidLocalhost_APIRequest(str2));
String str3 = "https:/"
+ "/shiptos.com:8081/booking" ;
System.out.println(
isValidLocalhost_APIRequest(str3));
String str4 = "www.gmail.com" ;
System.out.println(
isValidLocalhost_APIRequest(str4));
String str5 = "http:/"
+ "/localhost:30000/sends/search" ;
System.out.println(
isValidLocalhost_APIRequest(str5));
String str6 = "localhost:8000/students" ;
System.out.println(
isValidLocalhost_APIRequest(str6));
}
}
|
Python3
import re
def isValidLocalhost_APIRequest( str ):
regex = "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]" \
"*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\" \
"?\\, \'\\/\\\\\\+&%\\$#_]*)?$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
print (isValidLocalhost_APIRequest(str1))
print (isValidLocalhost_APIRequest(str2))
print (isValidLocalhost_APIRequest(str3))
str4 = "www.gmail.com"
print (isValidLocalhost_APIRequest(str4))
print (isValidLocalhost_APIRequest(str5))
str6 = "localhost:8000 / students"
print (isValidLocalhost_APIRequest(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG{
public static bool isValidLocalhost_APIRequest(String str)
{
var regex = new Regex( "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]"
+ "*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\"
+ "-\\.\\?\\, \'\\/\\\\\\+&%\\$#_]*)?$" );
if (str == null )
{
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
var str1 = "http:/" + "/localhost:8082/api/secure" ;
Console.WriteLine(GFG.isValidLocalhost_APIRequest(str1));
var str2 = "http:/" + "/localhost:1337/api/products" ;
Console.WriteLine(GFG.isValidLocalhost_APIRequest(str2));
var str3 = "https:/" + "/shiptos.com:8081/booking" ;
Console.WriteLine(GFG.isValidLocalhost_APIRequest(str3));
var str4 = "www.gmail.com" ;
Console.WriteLine(GFG.isValidLocalhost_APIRequest(str4));
var str5 = "http:/" + "/localhost:30000/sends/search" ;
Console.WriteLine(GFG.isValidLocalhost_APIRequest(str5));
var str6 = "localhost:8000/students" ;
Console.WriteLine(GFG.isValidLocalhost_APIRequest(str6));
}
}
|
Javascript
function isValidLocalhost_APIRequest(str)
{
let regex = new RegExp( "^(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\, \’\\/\\\\\\+&%\\$#_]*)?$" );
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "http:// localhost:1337/api/products" ;
console.log(isValidLocalhost_APIRequest(str1)+ "<br>" );
let str2 = "http:// localhost:1337/api/products" ;
console.log(isValidLocalhost_APIRequest(str2)+ "<br>" );
let str3 = "https:// shiptos.com:8081/booking" ;
console.log(isValidLocalhost_APIRequest(str3)+ "<br>" );
let str4 = "www.gmail.com" ;
console.log(isValidLocalhost_APIRequest(str4)+ "<br>" );
let str5 = "http:// localhost:30000/sends/search" ;
console.log(isValidLocalhost_APIRequest(str5)+ "<br>" );
let str6 = "localhost:8000/students" ;
console.log(isValidLocalhost_APIRequest(str6)+ "<br>" );
|
Output
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:
Last Updated :
24 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...