简体   繁体   中英

how to form an http post by python requests package

The doc is here: https://www.deepl.com/docs-api/translating-text/example/

POST /v2/translate?auth_key=[yourAuthKey]> HTTP/1.0

Host: api.deepl.com

User-Agent: YourApp

Accept: */*

Content-Length: 54

Content-Type: application/x-www-form-urlencoded

auth_key=[yourAuthKey]&text=Hello, world&target_lang=DE

How should I form the calling for python requests package? The following seems to be wrong. Thank you.

import requests
r = requests.post("https://api.deepl.com", data={"Content-Length": 54, "Content-Type": "application/x-www-form-urlencoded", "User-Agent": "YourApp", "auth_key": "XXXXXXXXXX&text=Hello, world&target_lang=DE"})

the curl example from the docs, translated to requests .

import requests

data = {'auth_key': 'yourAuthKey',
        'text': 'Hello, world',
        'target_lang': 'DE'}

response = requests.post('https://api.deepl.com/v2/translate', data=data)

If you want to add extra info like User-Agent , Content-Type , etc. then pass them in headers argument as dict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM