Rayrun
← Back to Discord Forum

apiContext with baseURL still seems to use the baseURL in the config

We've run into a situation where the response headers seem to indicate that the apiContext is calling the UI instead of the API URL. Would like to debug which URL is actually being called.

I've tried using the response.url() - Is this reporting the requested URL?

See the sample request and response below.

// assuming
// ui url: https://my.ui.url/ui <-- set as baseURL in playwright.config.ts
// api endpoint: https://my.api.url/api/test

const apiContext: APIRequestContext = playwright.request.newContext({
  baseURL: 'https://my.api.url/'
});
const response = await apiContext.get('/api/test');
console.log(response.url()) // gives https://my.ui.url/ui

This thread is trying to answer question "Why does the `apiContext` seem to be calling the UI URL instead of the API URL and how can this be debugged?"

2 replies

The response.url() will return the response url not the request url, so that is probably going to cause some confusion. You could look at the network console in UI mode to see what the get command is sending out. Another option is to intercept the route and see if you are getting any traffic from /api/test at that point you can determine the request url. I am using the same logic for my APIs and the routing is correct, so do not see any code issue. One thing, if you are requiring extraHTTPHeadsers in all your calls, you will need to specify that on the options in the newContext(). I found that this was not being configured from my global configuration.

If you are running your tests via CLI, then try using DEBUG=pw:api npx playwright test that'll log all request and responses in your terminal, including the url used. Good place to start. Also I noticed your baseURL ends with a backslash and the get endpoint starts with a backslash so you'll end up with https://my.api.url//api/test if I'm not wrong

Answer

Understanding response.url() and apicontext in @playwright/test

From the information we have, it's unclear what response.url() reports in @playwright/test. It's not explicitly described in the provided content links.

import { test } from '@playwright/test';

test('Check response URL', async ({ page }) => {
  const response = await page.goto('https://ray.run/');
  console.log(response.url());
});

As for apicontext, it's not a recognized entity in the @playwright/test documentation. You might be referring to "APIRequestContext" or "APIRequest", but without more context, it's hard to say why it would call a UI URL instead of an API URL.

import { test } from '@playwright/test';

test('APIRequestContext example', async ({ page }) => {
  // hypothetical APIRequestContext usage
});

For more information on how to use @playwright/test, you might find these blog posts helpful:

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].