如何重新格式化使用curlify lib请求的结果?

问题描述 投票:0回答:1

Pyrhon, GraphQL, Allure Reports, Requests.

你好。我需要将请求详细信息附加到我的报告中。 我使用curlify lib,但无法重新格式化结果以使其易于阅读。

import curlify
def def api_request(method='POST', **kwargs):
    with allure.step(f"{method.upper()}{base_url}"):
        with sessions.Session() as session:
            response = session.request(method=method, url=base_url, **kwargs)
            message = to_curl(response.request)

            allure.attach(
                body=message.encode("utf-8"),
                name="Curl",
                attachment_type=AttachmentType.TEXT,
                extension='txt'

我得到的附件看起来像:

curl -X POST -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'Content-Length: 318' -H 'Content-Type: application/json' -H 'User-Agent: python-requests/2.31.0' -d '{"query": "\n    mutation Mutation($email: String!, $password: String!) {\n    tokenAuth(email: $email, password: $password) {\n        token\n        refreshToken\n        refreshExpiresIn\n        __typename\n      }\n    }\n    ", "variables": {"email": "ABC", "password": "ABC"}}' https://url/api/v2/graphql/

但我至少想以这种方式重新格式化查询:

curl -X POST -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'Content-Length: 318' -H 'Content-Type: application/json' -H 'User-Agent: python-requests/2.31.0' -d '
{"query": " mutation Mutation($email: String!, $password: String!) 
   { tokenAuth(email: $email, password: $password) {        
      token        
      refreshToken        
      refreshExpiresIn        
      __typename      
   }    
}    ", 
"variables": {"email": "ABC", "password": "ABC"}}' https://url/api/v2/graphql/

需要你的帮助!

我尝试将结果转换为 JSON 并使用

json.dumps(response.json(), ensure_ascii=False, indent=4).encode("utf-8")
,但预计它不起作用。

python api graphql request attachment
1个回答
0
投票

message.replace(r'\n', '\n')
可以用来解决问题。 但是否有一些更简单的方法来重新格式化?

© www.soinside.com 2019 - 2024. All rights reserved.