在Python中使用API​​ POST方法上传Office Open XML文件

发布于 2025-01-09 14:13:34 字数 2796 浏览 0 评论 0原文

我正在尝试编写一个脚本来帮助我使用 CAT 工具 (Memsource) 自动执行一些工作。为此,我需要使用API​​上传一些文件。

我依赖此处提供的 Memsource API 文档: https://cloud.memsource.com /web/docs/api#operation/createJob

我编写了一段简短的代码来测试文件上传,然后再将其设为异步,并且我遇到了一些严重的问题:文本文件上传正确,尽管文本正文包含之后的一些补充上传中:

--4002a5507da490554ad71ce8591ccf69    
Content-Disposition: form-data; name="file"; filename=“test.txt"

我也尝试上传DOCX文件,但它甚至无法在Memsource在线编辑器中打开——我猜内容是一路修改的,但我找不到在哪里......

负责上传的代码如下:

def test_upload(self):
    # Assemble "Memsource" header as mentioned in the API docs
    Memsource_header = {
        "targetLangs": ["pl"],
    }

    # Open the file to be uploaded and extract file name
    f = open("Own/TMS_CAT/test.txt", "rb")
    f_name = os.path.basename(f.name)

    # Assemble the request header
    header = {
        "Memsource": json.dumps(Memsource_header),
        "Content-Disposition": f'attachment; filename="{f_name}"',
        "Authorization": f"ApiToken {self.authToken}",
        "Content-Type": "application/octet-stream; charset=utf-8",
    }

    # Make POST request and catch results
    file = {"file": f}

    req = requests.post(
        "https://cloud.memsource.com/web/api2/v1/projects/{project-id}/jobs",
        headers=header,
        files=file,
    )
    print(req.request.headers)
    print(req.json())

请求标头:

{
   "User-Agent":"python-requests/2.27.1",
   "Accept-Encoding":"gzip, deflate",
   "Accept":"*/*",
   "Connection":"keep-alive",
   "Memsource":"{\"targetLangs\": [\"pl\"]}",
   "Content-Disposition":"attachment; filename=\"test.txt\"",
   "Authorization":"ApiToken {secret}",
   "Content-Type":"application/octet-stream; charset=utf-8",
   "Content-Length":"2902"
}

以及 Memsource 的响应:

    {
   "asyncRequest":{
      "action":"IMPORT_JOB",
      "dateCreated":"2022-02-22T18:36:30+0000",
      "id":"{id}"
   },
   "jobs":[
      {
         "workflowLevel":1,
         "workflowStep":{
            "uid":"{uid}",
            "order":2,
            "id":"{id}",
            "name":"Tra"
         },
         "imported":false,
         "dateCreated":"2022-02-22T18:36:30+0000",
         "notificationIntervalInMinutes":-1,
         "updateSourceDate":"None",
         "dateDue":"2022-10-10T12:00:00+0000",
         "targetLang":"pl",
         "continuous":false,
         "jobAssignedEmailTemplate":"None",
         "uid":"{id}",
         "status":"NEW",
         "filename":"test.txt",
         "sourceFileUid":"{id}",
         "providers":[
            
         ]
      }
   ],
   "unsupportedFiles":[
      
   ]
}

对我来说两者看起来都不错...

我将不胜感激任何关于如何让这个东西工作的建议! :-)

I am trying to write a script to help me automate some work with our CAT tool (Memsource). To this end, I need to upload some files using API.

I rely on Memsource API documentation available here: https://cloud.memsource.com/web/docs/api#operation/createJob

I wrote a short code to test file uploading before moving to making it async, and I have some serious problem: text files are uploaded correctly, although the body of the text contains some additions after uploading:

--4002a5507da490554ad71ce8591ccf69    
Content-Disposition: form-data; name="file"; filename=“test.txt"

I also tried to upload DOCX file, but it cannot be even opened in Memsource online editor — I guess the content is modified along the way, but I am unable to find where...

The code responsible for the upload is as follows:

def test_upload(self):
    # Assemble "Memsource" header as mentioned in the API docs
    Memsource_header = {
        "targetLangs": ["pl"],
    }

    # Open the file to be uploaded and extract file name
    f = open("Own/TMS_CAT/test.txt", "rb")
    f_name = os.path.basename(f.name)

    # Assemble the request header
    header = {
        "Memsource": json.dumps(Memsource_header),
        "Content-Disposition": f'attachment; filename="{f_name}"',
        "Authorization": f"ApiToken {self.authToken}",
        "Content-Type": "application/octet-stream; charset=utf-8",
    }

    # Make POST request and catch results
    file = {"file": f}

    req = requests.post(
        "https://cloud.memsource.com/web/api2/v1/projects/{project-id}/jobs",
        headers=header,
        files=file,
    )
    print(req.request.headers)
    print(req.json())

The request header:

{
   "User-Agent":"python-requests/2.27.1",
   "Accept-Encoding":"gzip, deflate",
   "Accept":"*/*",
   "Connection":"keep-alive",
   "Memsource":"{\"targetLangs\": [\"pl\"]}",
   "Content-Disposition":"attachment; filename=\"test.txt\"",
   "Authorization":"ApiToken {secret}",
   "Content-Type":"application/octet-stream; charset=utf-8",
   "Content-Length":"2902"
}

And the response from Memsource:

    {
   "asyncRequest":{
      "action":"IMPORT_JOB",
      "dateCreated":"2022-02-22T18:36:30+0000",
      "id":"{id}"
   },
   "jobs":[
      {
         "workflowLevel":1,
         "workflowStep":{
            "uid":"{uid}",
            "order":2,
            "id":"{id}",
            "name":"Tra"
         },
         "imported":false,
         "dateCreated":"2022-02-22T18:36:30+0000",
         "notificationIntervalInMinutes":-1,
         "updateSourceDate":"None",
         "dateDue":"2022-10-10T12:00:00+0000",
         "targetLang":"pl",
         "continuous":false,
         "jobAssignedEmailTemplate":"None",
         "uid":"{id}",
         "status":"NEW",
         "filename":"test.txt",
         "sourceFileUid":"{id}",
         "providers":[
            
         ]
      }
   ],
   "unsupportedFiles":[
      
   ]
}

both look okay to me...

I will appreciate any suggestions on how to get this thing working! :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

城歌 2025-01-16 14:13:34

我设法解决了这个问题 - 注意到请求在请求正文中添加了一些有限的标头,即在 files 参数中传递的文件内容。

我只是摆脱了它并更改了代码,如下所示:

# Open the file to be uploaded and extract file name
        with open(
            "/file.ext", "rb"
        ) as f:
            f_name = os.path.basename(f.name)

            # Assemble the request header
            header = {
                "Memsource": json.dumps(Memsource_header),
                "Content-Disposition": f'attachment; filename="{f_name}"',
                "Authorization": f"ApiToken {self.authToken}",
                "Content-Type": "application/octet-stream; charset=utf-8",
            }

            req = requests.post(
                "https://cloud.memsource.com/web/api2/v1/projects/{project-id}/jobs",
                headers=header,
                data=f,
            )

I managed to fix this problem — noticed that requests are adding some limited headers to the body of the request, i.e., the content of the file passed in files parameter.

I simply got rid of that and changed the code as follows:

# Open the file to be uploaded and extract file name
        with open(
            "/file.ext", "rb"
        ) as f:
            f_name = os.path.basename(f.name)

            # Assemble the request header
            header = {
                "Memsource": json.dumps(Memsource_header),
                "Content-Disposition": f'attachment; filename="{f_name}"',
                "Authorization": f"ApiToken {self.authToken}",
                "Content-Type": "application/octet-stream; charset=utf-8",
            }

            req = requests.post(
                "https://cloud.memsource.com/web/api2/v1/projects/{project-id}/jobs",
                headers=header,
                data=f,
            )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文