From 4c130636d3fbaca5d1984dd11418db70e0b66865 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Guedes Date: Fri, 21 Jan 2022 10:58:45 -0300 Subject: [PATCH] Fixing the request method. --- api/apiproxy/cli.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api/apiproxy/cli.py b/api/apiproxy/cli.py index ae9052f..4d971ac 100644 --- a/api/apiproxy/cli.py +++ b/api/apiproxy/cli.py @@ -1,3 +1,4 @@ +import base64 import json import os import requests @@ -35,12 +36,18 @@ class API_Proxy: headers = {"X-API-KEY": apikey} res = requests.post(url, json = object, headers = headers, verify = False) - if res.status_code >= 400: - raise Exception(f"HTTP ERROR: {str(res.status_code)} - {res.text}") - if res.headers.get("Content-Type") != None and res.headers.get("Content-Type").find("json") != -1: - return json.loads(res.text) + response = json.loads(res.text) + + byte_encoded_body = base64.b64decode(response['body']) + decoded_body = str(byte_encoded_body, "utf-8") + + if response['status_code'] >= 400: + raise Exception(f"HTTP ERROR: {str(response['status_code'])} - {response['body']}") + + if response['header']['Content-Type'] != None and response['header']['Content-Type'].find('json') != -1: + return json.loads(decoded_body) else: - return res.text + return decoded_body def do (self, request: dict): """