Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: Java, WebDriver not adding Content-Type header when doing calls from any browser towards backend #15530

Open
MichalLipski95 opened this issue Mar 28, 2025 · 1 comment · May be fixed by #15535
Labels
A-needs-triaging A Selenium member will evaluate this soon! C-java Java Bindings I-defect Something is not working as intended

Comments

@MichalLipski95
Copy link

MichalLipski95 commented Mar 28, 2025

What happened?

I am using following setup of chrome driver:

import io.github.bonigarcia.wdm.WebDriverManager;   
 public ChromeDriver configureChromeDriver() {
        WebDriverManager.chromedriver().setup();
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--disable-extensions");

        ChromeDriver chromeDriver = (ChromeDriver) 
        WebDriverManager.chromedriver().capabilities(chromeOptions).create();
        Headers headers = new Headers(getCustomHeaders());
        DevTools devTools = chromeDriver.getDevTools();

        devTools.createSession();
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        // Create request patterns to intercept
        List<RequestPattern> patterns = new ArrayList<>();

        // Intercept HTTPS requests for specific URLs or resource types
        patterns.add(new RequestPattern(
                Optional.of(BASE_URI + "/*"),  // URL pattern
                Optional.empty(),    // Resource type
                Optional.empty()                       // Request stage (optional)
        ));


        devTools.send(Network.setRequestInterception(patterns));

        devTools.addListener(Network.requestIntercepted(), request -> {
            // Check if headers should be added
            if(request.getRequest().getUrl().contains(BASE_URI + "/*")){

             devTools.send(Network.continueInterceptedRequest(
                request.getInterceptionId(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.of(headers),
                Optional.empty()
        ));

            } else {
                // Continue the request without modifications
                devTools.send(Network.continueInterceptedRequest(
                        request.getInterceptionId(),
                        Optional.empty(),
                        Optional.empty(),
                        Optional.empty(),
                        Optional.empty(),
                        Optional.empty(),
                        Optional.empty(),
                        Optional.empty()
                ));
            }
        });
        chromeDriver.manage().window().maximize();
        return chromeDriver;
    }
    private Map<String, Object> getCustomHeaders() {
        Map<String, Object> headers = new HashMap<>();
        headers.put(EMAIL_KEY, EMAIL);
        return headers;
    }

Later I am using this configured driver in the test as follwos:

ChromeDriver driver = configureChromeDriver();
MyPage myPage = new MyPage(driver);

// Later in this part I am opening page with driver.get(myUri) and clicking through my web application.
webPage.openWebPage(BASE_URI);

Once there is a situation that browser is making call to backend as POST, PUT or PATCH there is no Content-Type header at all which is returning errors from backend. I can observe that my custom headers and many other default headers are available.

Dependencies:

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.30.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>6.0.0</version>
        </dependency>

I am attaching request headers from manual tests in Chrome browser as well request headers which are provided from driver managed chrome browser

Manual Chrome headers:
Image

WebDriver managed Chrome headers:
Image
Image

How can we reproduce the issue?

Please use WebDriver configuration as mentioned in above code snippet and run any page. Once test is running and you see place on webpage that triggers call to backend with required content type header add Thread.sleep for some long time that you can open developer tools and click in driver managed browser the button which will trigger call to backend. In Network Section check for Request Headers and there you can see that Content-type is not being added.

Relevant log output

No exception appearing, just missing Content-type header.

Operating System

Windows10

Selenium version

Java 21

What are the browser(s) and version(s) where you see this issue?

Chrome - 134

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 134.0.6998.165

Are you using Selenium Grid?

No

@MichalLipski95 MichalLipski95 added I-defect Something is not working as intended A-needs-triaging A Selenium member will evaluate this soon! labels Mar 28, 2025
Copy link

@MichalLipski95, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@navin772 navin772 added the C-java Java Bindings label Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-needs-triaging A Selenium member will evaluate this soon! C-java Java Bindings I-defect Something is not working as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants