0

I'm trying to get some data from the api using python requests. My code is as follows:

response = settings.get_request_with_token(settings.api_url, settings.token)
dashboard = json.loads(response.decode("utf-8"))

And get_request_with_token is as follows:

def post_request_with_token(url, api_key, json_payload={}):
    r = requests.post(url, headers={'Authorization': 'Bearer ' + api_key}, data=json_payload)
    return r.content

In my response I get the following result:

'[{"id":1,"title":"Name","uri":"db/name","type":"dash-db","tags":[],"isStarred":false}]'

I use then json.loads to get the following result:

[{u'tags': [], u'type': u'dash-db', u'title': u'Name', u'uri': u'db/name', u'isStarred': False, u'id': 1}]

But I cannot rid of 'u' .

I tried as you can see with .decode("utf-8") but I get the same.

Any idea how to solve it?

UPDATE

I'm trying to update dashboard on Grafana and I'm getting the following error:

[{u'message': u"invalid character 'm' looking for beginning of value", u'classification': u'DeserializationError'}, {u'message': u'Required', u'classification': u'RequiredError', u'fieldNames': [u'Dashboard']}]

UDPDATE2

The whole script looks as follows:

try:
    dashboard_name = sys.argv[1]  # Get the argument

    # Get dashboard from QA organisation (we give default api token. The default token is from QA organisation from grafana)
    response = settings.get_request_with_token(settings.api_url + "search?query=" + dashboard_name, settings.token)
    dashboard = json.loads(response.decode("utf-8"))

    if len(dashboard) < 1:
        print("There is no dashboard data.")
    else:
        dashboard_data = dashboard[0]
        dashboard_uri = str(dashboard_data["uri"])  
        dashboard_details = dashboard_api.get_dashboard(dashboard_uri, settings.token)
        # Get all organisations
        all_organisations = organization_api.get_all_organizations()  # Get all organisations
        for org in all_organisations:
            if org['id'] == 28:
                org_data_list = filter(lambda organisation: organisation['grafana_id'] == org['id'], settings.organisations)
                try:
                    current_org_api_key = org_data_list[0]['api_key']

                    result = dashboard_api.add_dashboard(current_org_api_key, dashboard_details["dashboard"])
                    print(result)
                    pdb.set_trace()
                except:
                    pass
except IndexError:
    print("Please provide dashboard name!")

Function get_request_with_token looks as follows:

def get_request_with_token(url, api_key):
    r = requests.get(url, headers={'Authorization': 'Bearer ' + api_key})
    return r.content

Function get_dashboard looks as follows:

def get_dashboard(board_uri, token):
    response = settings.get_request_with_token(settings.api_url + "dashboards/{0}".format(board_uri), token)
    return json.loads(response.encode("utf-8"))

Function add_dashboard looks as follows:

def add_dashboard(token, json_payload):
    response = settings.post_request_with_token(settings.api_url + "dashboards/db", token, json_payload)
    return json.loads(response)

And function post_request_with_token looks as follows:

def post_request_with_token(url, api_key, json_payload={}):
    r = requests.post(url, headers={'Authorization': 'Bearer ' + api_key, 'Content-Type': 'application/json'}, data=json_payload)
    return r.content
Boky
  • 11,554
  • 28
  • 93
  • 163
  • 1
    'u' just stands for Unicode. I don't think it will cause any problems – Rakesh Mar 15 '18 at 07:16
  • @Rakesh It causing the problems unfortunately – Boky Mar 15 '18 at 07:18
  • 'u' just stands for Unicode; it's not causing problems. Post your error. – smci Mar 15 '18 at 07:23
  • 3
    https://stackoverflow.com/questions/7784148/understanding-repr-function-in-python and https://stackoverflow.com/questions/9773121/removing-u-in-list. You would not `decode()` unicode, as it is already decoded. You'd `encode()` when you need the bytes. – Ilja Everilä Mar 15 '18 at 07:23
  • 2
    post your complete script, please? – hd1 Mar 15 '18 at 07:24
  • have you tried `ast.literal_eval(json.dumps(list))` ? here the list is your final result gets from json.loads – Vikas Periyadath Mar 15 '18 at 07:25
  • @IljaEverilä with `.encode("utf-8")` I get the same. – Boky Mar 15 '18 at 07:31
  • That was not meant as a solution, but just a note on unicode vs. bytes. You haven't given enough information to solve your real issue, which most probably has nothing to do with unicode vs. bytes, though it might. – Ilja Everilä Mar 15 '18 at 07:33
  • I think you will get more detail here:https://stackoverflow.com/questions/9773121/removing-u-in-list?noredirect=1&lq=1 – yogesh10 Mar 15 '18 at 07:36

0 Answers0