playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

[Feature]: Submit the issue of duplicate key-value pairs in the form

Open KaHuier opened this issue 7 months ago • 0 comments

🚀 Feature Request

Submit the issue of duplicate key-value pairs in the form

Example

The data required by the server interface is as follows:
test=1&test=2&test=3&mc=kaka

The implementation plan for sending requests is outlined below:

import requests

response = requests.post(
    url='http://127.0.0.1:5000/form',
    verify=False,
    data={
        "test": ["1", "2", "3"],
        "mc": "kaka"
    }
)
print(response.text)

It should be noted that Playwright does not natively support passing repeated key-value pairs, as they are escaped during serialization. To address this limitation, the following temporary solution has been implemented:

from typing import Optional, List, Dict
from playwright._impl._helper import NameValue


def object_to_array(obj: Optional[Dict]) -> Optional[List[NameValue]]:
    if not obj:
        return None
    result = []
    for key, value in obj.items():
        if isinstance(value, (tuple, list)):
            for v in value:
                result.append(NameValue(name=key, value=str(v)))
        else:
            result.append(NameValue(name=key, value=str(value)))
    return result


_helper.object_to_array = object_to_array

from playwright.sync_api import sync_playwright


playwright = sync_playwright().start()
browser = playwright.chromium.launch()
page = browser.new_page()
response = page.request.post(
    url='http://127.0.0.1:5000/form',
    form={
        "test": ["1", "2", "3"],
        "mc": "kaka"
    }
)
print(response.text)

Motivation

When encountering a duplicate key-value pair that needs to be submitted in the form in the interface

KaHuier avatar Apr 29 '25 02:04 KaHuier