How to reformat result of using curlify lib for request?

How to reformat result of using curlify lib for request?


0

Pyrhon, GraphQL, Allure Reports, Requests.

I need to attach request details to my repotr.
I use curlify lib, but can’t reformat result to make in easy to read.

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'

And I get attach looking like:

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        tokenn        refreshTokenn        refreshExpiresInn        __typenamen      }n    }n    ", "variables": {"email": "ABC", "password": "ABC"}}' https://url/api/v2/graphql/

But I would like at least to reformat query this way:

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/

Need your help!

I’ve tried to convert result to JSON and use json.dumps(response.json(), ensure_ascii=False, indent=4).encode("utf-8"), but expectably it didn’t work.

New contributor

Katrin T is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1 Answer
1


0

message.replace(r'n', 'n') can be used to resolve the problem.
But if there some easier way to reformat?

New contributor

Katrin T is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *