Rayrun
← Back to Discord Forum

How can I send an array with values for a single key with APIRequestContext's POST and using Form?

Hey everyone! I'm currently struggling with a scenario, where I'm forced to send a POST request with Content-Type value of application/x-www-form-urlencoded, concisely speaking, sending a form. However I have to send multiple values for a singular key, scope. The Playwright documentation states that I can't pass an array as a value for a key within an object: https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-post-option-form Additionally, using the same key name multiple times in an object will override previous values.

For instance, I want to achieve something like this while sending form data:

{
  scope: "value1",
  scope: "value2",
  scope: "value3"
}

Given these constraints, how can I effectively send multiple values for the "scope" key?

This thread is trying to answer question "How can I send multiple values for a single key with APIRequestContext's POST and using Form?"

6 replies

Can you pass scope as an array? Does the api accept a delimited list as in values separated by a space or comma?

Your code would look like

Scope: { value, value, value}

Or

Scope: "value value value"

Sorry for formatting on a train.

Answer

Sure, you can send multiple values for a single key using APIRequestContext's post method in @playwright/test. Here's how you can do it:

await request.post('https://ray.run/api/endpoint', {
  form: {
    key: ['value1', 'value2', 'value3'],
  },
});

In this code snippet, we're sending three values (value1, value2, and value3) for the parameter key. The server will receive these as separate entries under the same parameter name.

To send multiple values, you just need to provide an array of values for a single key. Remember to use square brackets ([]) after the parameter name in your object to indicate it's an array.

However, keep in mind that this method assumes that the server-side API endpoint can accept multiple values for a single key. Always check your API documentation or ask your backend team to ensure this method is compatible.

If you need more help, don't hesitate to ask!

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].