BostonAreaHuman
BostonAreaHuman

Reputation: 1461

No Matter What I do I can't get the outbound header to x-url-form-encoded using Requests Python

The recipient needs to have x-url-form-encoded arrived at the receiver. No matter what I do the header is stuck at Content-Type: application/json

data2 = {"grant_type": "refresh_token", "refresh_token": config_keys['cc_api_refresh'],"client_id": config_keys['client_id']}
    print(type(data2))
    print('pringting data')
    print(data2)
    dorefresh = requests.post(config_urls['cc_refresh_url'],data=data2,headers={"Content-Type": "application/x-www-form-urlencoded"})
    for header, value in dorefresh.headers.items():
        print(f'{header}: {value}')

and the result

pringting data
{'grant_type': 'refresh_token', 'refresh_token': 'xx', 'client_id': 'xxxxx'}
Date: Wed, 26 Feb 2025 02:37:20 GMT
Content-Type: application/json
Content-Length: 172
Connection: keep-alive

Upvotes: -1

Views: 26

Answers (1)

Suramuthu R
Suramuthu R

Reputation: 1913

As the recipient needs to have x-url-form-encoded arrived at the receiver, the better way is manually encoding data.

# import urllib.parse
import urllib.parse 

import requests    

data2 = {"grant_type": "refresh_token", "refresh_token": config_keys['cc_api_refresh'],"client_id": config_keys['client_id']}
    print(type(data2))
    print('pringting data')
    print(data2)

# manually encode
encoded = urllib.parse.urlencode(data2) 

response = requests.post(config_urls['cc_refresh_url'], data=encoded, headers=headers)

# no need to iterate
print(dorefresh.headers)

Upvotes: 0

Related Questions