【问题标题】:Unable to upload file in locust无法在蝗虫中上传文件
【发布时间】:2021-02-27 07:34:19
【问题描述】:
    @task
    def constraintsuploaddoc(self, false=None):

        headersc = {'content-type': 'multipart/form-data; '
                                    'boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Content-Length': '433', 'Accept': '*/*'}
        payload = {
            'blobText': '{ ID: 3, FileClass: "LogicsticsConstraints", FileDescriptor: "test", FieldName: '
                        '"lookup.csv" }'}
        files = [
            ('File',
             ('lookup.csv', open(r"C:\Perffiles\lookup.csv",'rb'), 'text/csv'))
             ]
        rupload = self.client.put("/api/v1/FileUpload/Upload", data=payload,
                                  files=files,
                                  verify=False,
                                  headers=headersc, name="constraintsuploaddoc")

        if rupload.status_code == 200:
            # we got a 200 OK
            rupload.success()
        else:
            print(rupload)
            rupload.failure("Error occured")

postman request

错误:

    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-415d71c9881187498929206abd2ec4ab-9f31d5c108f99944-00",
    "errors":{
        "": ["Failed to read the request form. Unexpected end of Stream, the
 content may have already been read by another component. "]
    }
}

【问题讨论】:

  • 从使用 Python Requests 模块开始工作。然后,您应该几乎可以替换掉 Locust 客户端了。

标签: python performance-testing multipartform-data locust


【解决方案1】:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import json

multipart_data = MultipartEncoder(
fields={
    # a file upload field
    'file': ('lookup.csv', open(r'C:\Perffiles\lookup.csv', 'rb'), 'text/plain'),
    # plain text fields
    'blobText': '{ ID: 3, FileClass: "LogicsticsConstraints", FileDescriptor: "test", 
      FieldName: "aep_lookup -test1.csv" }'
    }
    )

  urlupload = "https://1.2.3.4/api/v1/FileUpload/Upload"
 headersc = {'content-type': multipart_data.content_type,
        'Host': '1.2.3.4', 'Accept': '*/*'}
response = requests.request("PUT", urlupload, headers=headersc, data=multipart_data, verify=False)

【讨论】:

    【解决方案2】:

    也许你正在使用 FastHTTPUser 解决这个问题

    https://github.com/locustio/locust/issues/1640

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2021-05-25
      • 1970-01-01
      相关资源
      最近更新 更多