Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zhenzhen1022 opened this issue May 30, 2019 · 2 comments
Open

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

zhenzhen1022 opened this issue May 30, 2019 · 2 comments

Comments

@zhenzhen1022
Copy link

  • 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
Copy link
Author

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

@zhenzhen1022
Copy link
Author

zhenzhen1022 commented May 30, 2019

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

@debugtalk debugtalk transferred this issue from httprunner/httprunner Jul 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant