I want to fetch the Payload part of a network call using selenium 4 Chrome dev Tools concept. I use selenium with java - eclipse as ide and Maven as run time environment.
Want to know the Command that is used to fetch network payload
I want to fetch the Payload part of a network call using selenium 4 Chrome dev Tools concept. I use selenium with java - eclipse as ide and Maven as run time environment.
Want to know the Command that is used to fetch network payload
you can use chrome dev tools library with selenium 4.
Step 1: Fetch all network requests.
ArrayList<Request> requests = new ArrayList<>();
chromeDevTools.addListener(Network.requestWillBeSent(),
entry -> {
requests.add(entry.getRequest());
});
Step 2: Iterate in requests list, and fetch post data of each post request, using regex split the string and further apply regex to get name fields. For getting the value of each name field, use a boolean flag. Capture each pair in a map.
for(Request r:requests){
if(r.getMethod().equals("POST") && r.getHasPostData().isPresent() && r.getHasPostData().get()) {
HashMap<String,String> reqPostDataM = new HashMap<>();
//parse multipart form data from input stream
//customrequest.setpostData(r.getPostData().get());
String[] postDataNL = r.getPostData().get().split("\r\n");
int reqBodyKeyFlag = 0;
ArrayList<String> keys = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
for(String eachLine:postDataNL) {
eachLine = eachLine.trim();
//System.out.println(eachLine);
String reqBodyKey = getNameFieldsRegex(eachLine);
if(reqBodyKey.length()>0) {
if(reqBodyKeyFlag==0)
{
reqBodyKey = reqBodyKey.substring(1,reqBodyKey.length()-1);
keys.add(reqBodyKey);
reqBodyKeyFlag = 1;
}
}
else if(reqBodyKeyFlag==1) {
if(eachLine.length()>0) {
System.out.println(eachLine);
values.add(eachLine);
reqBodyKeyFlag = 0;
}
}
}
System.out.println("keys_Size: "+keys.size());
System.out.println("values_Size: "+values.size());
for(int itr=0;itr<keys.size();itr++) {
reqPostDataM.put(keys.get(itr), values.get(itr));
}
}
PostRequest Payload format :
------WebKitFormBoundarySOSBRnoiHIwWKoVB
Content-Disposition: form-data; name="firstname"
Nishant
------WebKitFormBoundarySOSBRnoiHIwWKoVB
Content-Disposition: form-data; name="lastname"
Bhat
------WebKitFormBoundarySOSBRnoiHIwWKoVB
Content-Disposition: form-data; name="email"
bhatianishant@gmail.com
------WebKitFormBoundarySOSBRnoiHIwWKoVB
Content-Disposition: form-data; name="telephone"
9012054765
------WebKitFormBoundarySOSBRnoiHIwWKoVB--
Edit getNameFieldsRegexFunction:
public static String getNameFieldsRegex(final String input) {
// Compile regular expression
final Pattern pattern = Pattern.compile("\"[^\"]*\"", Pattern.CASE_INSENSITIVE);
// Match regex against input
final Matcher matcher = pattern.matcher(input);
// Use results...
String ans = "";
// System.out.println(input);
while(matcher.find()) {
int start_idx = matcher.start();
int len = input.length();
System.out.println(start_idx);
ans = input.substring(start_idx);
break;
}
return ans;
}