Convert cURL request to Python-Requests request

前端 未结 2 558
陌清茗
陌清茗 2021-01-20 13:28

I want to convert this cURL request to a Python-Requests request since I am working on a Python wrapper for a REST service

MS_WORD_DOCUMENT=...
CONTENT_TYPE=         


        
2条回答
  •  不思量自难忘°
    2021-01-20 14:15

    The Curl command sends specific multipart/form-data field names; meta and data, and the documentation for the API specifies specific meta-types to be used.

    Moreover, the metadata should be encoded to JSON.

    The following should work:

    import json
    import requests
    
    metadata = json.dumps({"documentType": "application/msword"})
    files = {
        'meta': ('', metadata, 'application/json'),
        'data': ('sample.docx', content, 'application/octet-stream'),
    }
    
    req = requests.post(
        "https://text.s4.ontotext.com/v1/twitie",
        auth=("user", "pass"),
        files=files)
    

    The files parameter is all that is needed here; each value is a tuple with a filename, the data to be sent, and the mimetype for that part.

提交回复
热议问题