Home > Software design >  Attribute Error for variable header passed in Post Request
Attribute Error for variable header passed in Post Request

Time:07-15

I get an AttributeError when I try to pass the header for making a post request to get a Token.

Code:

import requests

endpoint = 'https://api.website/v1/token'
header ={
    'Content-Type: application/x-www-form-urlencoded',
    'Cache-Control: no-cache'
}
data = {
    'username=<username>',
    'password=<password>',
    'grant_type=password',
    'client_id=api'
}
request = requests.post(endpoint, headers=header, data=data, verify=False)
print(request)

The cURL by using --data-urlencode:

curl -X POST \
  https://api.website/v1/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Cache-Control: no-cache' \
  --data-urlencode username=<YourUsername> \
  --data-urlencode password=<YourPassword> \
  --data-urlencode grant_type=password \
  --data-urlencode client_id=token-api

Error:

Traceback (most recent call last):
  File "c:\Learning\API.py", line 14, in <module>
    request = requests.post(endpoint, headers=header, data=data, verify=False)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 484, in prepare_request
    p.prepare(
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 369, in prepare
    self.prepare_headers(headers)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 489, in prepare_headers
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)                                   t/API.py
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)                                      False)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessio", line 115, in postns.py", line 573, in request
    prep = self.prepare_request(req)                                                              ", line 59, in request
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 484, in prepare_request                                                              ns.py", line 573, in request
    p.prepare(
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\modelsns.py", line 484, in prepare_request.py", line 369, in prepare
    self.prepare_headers(headers)                                                                 .py", line 369, in prepare
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 489, in prepare_headers  
AttributeError: 'set' object has no attribute 'items'

I tried to change the single quotes with double quotes,but it will have the same Error.

I also tried to use the curl withoout the --data-urlencode:

-d 'username=<YourUsername>&password=<YourPassword>&grant_type=password&client_id=token-api'

code:

data = {
'username=<YourUsername>&password=<YourPassword>&grant_type=password&client_id=token-api'
}

But got the same error

CodePudding user response:

Your code has a simple mistake with the headers… possibly also with the data, but I’m not sure on that one. Below is the code with inline explanations.

import requests

endpoint = 'https://api.website/v1/token'
# Below you have a set and it is expecting a dictionary
# header = {
#     'Content-Type: application/x-www-form-urlencoded',
#     'Cache-Control: no-cache'
# }

# Below is a dictionary, notice I only added quotes before and after the colon.
# Now it has the keys and values like "name_of_header": "value"
# single or double quotes do not matter here.
header = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Cache-Control': 'no-cache'
}
data = {
    'username=<username>',
    'password=<password>',
    'grant_type=password',
    'client_id=api'
}
request = requests.post(endpoint, headers=header, data=data, verify=False)
print(request)
  • Related