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

[Feature] Ability to handle multiple values for a singular key in .fetch() with APIRequestContext #26520

Closed
szuszik opened this issue Aug 17, 2023 · 0 comments · Fixed by #32602

Comments

@szuszik
Copy link

szuszik commented Aug 17, 2023

I'm working on a project that requires sending POST requests with 'application/x-www-form-urlencoded' encoding, specifically handling multiple values for a singular key (e.g., 'scope'). This is a common pattern in OAuth2 authorization and other API interactions, where you need to, for example, define authorization grant types.

Currently, I can achieve this with Fetch in JavaScript, but I'm unable to find a way to replicate this behavior using Playwright's APIRequestContext class.

let requestHeaders = new Headers();
requestHeaders.append("Content-Type", "application/x-www-form-urlencoded");

let urlEncoded = new URLSearchParams();
urlEncoded.append("scope", "value1");
urlEncoded.append("scope", "value2");
urlencoded.append("scope", "value3");
urlencoded.append("scope", "value4");
// and so on

let requestOptions = {
  method: 'POST',
  headers: requestHeaders,
  body: urlEncoded,
  redirect: 'no-follow'
};

fetch("https://example.com/authorize", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

However, I'm struggling to find a way to replicate this behavior using the APIRequestContext class in Playwright. Here's what I've tried so far:

await apiRequestContext.post('https://example.com/authorize', {
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  form: {
    "response_type": "auth_code",
    "scope": "value1", // How to handle multiple values for 'scope'?
    // rest of the parameters
  }
});

Since currently there's no way to handle multiple values for a singular key like 'key' using the APIRequestContext, I think it would be beneficial to be able to provide multiple values for one key, in a similar fashion it is achieved by URLSearchParams. It could be, for example, passed in an array of strings.

@szuszik szuszik changed the title [Feature] Ability to handle multiple values for a singular key in .fetch() with APIRequestContext [Feature] Ability to handle multiple values for a singular key in .fetch() with APIRequestContext Aug 17, 2023
@Skn0tt Skn0tt self-assigned this Sep 13, 2024
@Skn0tt Skn0tt added the v1.48 label Sep 13, 2024
Skn0tt added a commit that referenced this issue Sep 13, 2024
Closes #26520 by accepting
`FormData`, which became stable in Node.js in v21.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants