0

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?

Thomas
  • 192
  • 2
  • 10

2 Answers2

2

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.

Thomas
  • 192
  • 2
  • 10
2

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),
    ]
)
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
  • 1
    Thank you for your answer! I think the method of passing a list of strings to `text` is a bit more simple, so I'm going to go ahead and accept that answer -- but thank you for your help! – Thomas Jun 06 '21 at 18:14