postman2case icon indicating copy to clipboard operation
postman2case copied to clipboard

UnicodeEncodeError: 'latin-1' codec can't encode chinese

Open zhenzhen1022 opened this issue 5 years ago • 2 comments

  • OS:Windows
  • Python 3.6
  • HttpRunner 2.0 when the request contains chinese, I got the error massege as following. how can I fix it?

Traceback (most recent call last): UnicodeEncodeError: 'latin-1' codec can't encode characters in position 17-18: Body ('上海') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.


Ran 1 test in 0.004s

zhenzhen1022 avatar May 30 '19 03:05 zhenzhen1022

@debugtalk can you hava a look at this question,thx

zhenzhen1022 avatar May 30 '19 03:05 zhenzhen1022

The testcases are genereated by postman2case, and I find the solution by update core.py in postman2case. location: postman2case/core.py:76 solution: add "body = json.loads(body, encoding='utf-8')" and change "request["data"] = body" to "request["json"] = body" . now, the question is solved.

def parse_each_item(self, item):
    """ parse each item in postman to testcase in httprunner
    """
    api = {}
    api["name"] = item["name"]
    api["validate"] = [{"check": "status_code", "comparator": "eq", "expect": 200},{"eq": ["content.ResponseStatus.Ack", "Success"]}]
    api["variables"] = []

    request = {}
    request["method"] = item["request"]["method"]

    url = self.parse_url(item["request"]["url"])

    if request["method"] == "GET":
        request["url"] = url.split("?")[0]
        request["headers"] = self.parse_header(item["request"]["header"])

        body = {}
        if "query" in item["request"]["url"].keys():
            for query in item["request"]["url"]["query"]:
                api["variables"].append({query["key"]: parse_value_from_type(query["value"])})
                body[query["key"]] = "$"+query["key"]
        request["params"] = body
    else:
        request["url"] = url
        request["headers"] = self.parse_header(item["request"]["header"])

        body = {}
        if item["request"]["body"] != {}:
            mode = item["request"]["body"]["mode"]
            if isinstance(item["request"]["body"][mode], list):
                for param in item["request"]["body"][mode]:
                    if param["type"] == "text":
                        api["variables"].append({param["key"]: parse_value_from_type(param["value"])})
                    else:
                        api["variables"].append({param["key"]: parse_value_from_type(param["src"])})
                    body[param["key"]] = "$"+param["key"]
            elif isinstance(item["request"]["body"][mode], str):

                body = item["request"]["body"][mode]
                body = json.loads(body, encoding='utf-8')
        request["json"] = body

    api["request"] = request
    return api

zhenzhen1022 avatar May 30 '19 08:05 zhenzhen1022