How can multiple "text" parameters be passed to the DeepL API using Python?

1k Views Asked by At

I am trying to pass multiple text variables with the DeepL API using Python 3's requests library, but as yet have been unable to pass multiple text parameters to a request. The documentation states that "Up to 50 text parameters can be submitted in one request" but after trying to pass multiple text variables using "&text": and the regular "text": keys, the call only returns a single translation.

For reference, here is the code I used:

two_s = r.post(url="https://api.deepl.com/v2/translate", data={"target_lang": "EN", "auth_key": auth_key, "text": s2t1, "text":s2t2})

and

two_s = r.post(url="https://api.deepl.com/v2/translate", data={"target_lang": "EN", "auth_key": auth_key, "text": s2t1, "&text":s2t2})

Where auth_key is my authentication key, and s2t1 and s2t2 are different "strings 2 translate." How can multiple "text": parameters be passed to the DeepL API using Python?

2

There are 2 best solutions below

0
On BEST ANSWER

Edit: I attempted to pass s2t1 and s2t2 to text as a list of elements, as outlined in Martijn Pieters (Martijn Pieters) answer to this question and both strings were translated and returned by the server. Here is the code I used to make that request:

both_s = r.post(url="https://api.deepl.com/v2/translate", data={"target_lang": "EN", "auth_key": auth_key, "text": [s2t1,s2t2]})

Where auth_key is my DeepL authentication key.

Edit: It is also worth noting that DeepL has an ~5000 character limit that it can translate at a time, so while multiple strings can be passed to the text parameter, it is easier to simply pass the entire string along. I will do some efficiency analysis of passing a full 4800 character string in text and a test passing a split list of strings and update further.

1
On

You can pass a list of tuples as data attribute.
After some test with some echo services it seems to work with several values for the same "key".

r.post(
    url="https://api.deepl.com/v2/translate", 
    data=[
        ("target_lang", "EN"),
        ("auth_key", auth_key),
        ("text", s2t1),
        ("text", s2t2),
    ]
)