Search code examples
python-requestshttp-post

How to write this HTTP request in python?


I need to write this HTTP request. I was able to get the token (from another HTTP request) and I saved on in a variable name "Token"

Can you please help me write this HTTP request in python? I am getting 404 and I am pretty sure the syntax is wrong.

Attaching a screenshot from Microsoft documentation.

enter image description here

the new error message:

Traceback (most recent call last):
  File "/Users/talcohen/opt/anaconda3/lib/python3.9/site-packages/requests/models.py", line 972, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Users/talcohen/opt/anaconda3/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/Users/talcohen/opt/anaconda3/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/talcohen/opt/anaconda3/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/talcohen/Documents/ddd/PowerBI_REST_API/CreateProfile.py", line 15, in <module>
    data = requests.post(api_url, json=payload, headers=headers).json()
  File "/Users/talcohen/opt/anaconda3/lib/python3.9/site-packages/requests/models.py", line 976, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
(base) talcohen@Tals-Mac-mini PowerBI_REST_API % 

Solution

  • Looking at the API you should probably do:

    import requests
    
    token = "<YOUR TOKEN>"
    api_url = "https://api.powerbi.com/v1.0/myorg/profiles"
    headers = {"Authorization": f"Bearer {token}"}
    
    payload = {"displayName": "My First Profile"}
    
    data = requests.post(api_url, json=payload, headers=headers).json()
    print(data)