Python Requests gives Invalid Return Character or Leading Space in Header On Line Breaks

Viewed 2341

Python 3.8.5 Requests 2.22.0

The API I'm working with requires a unique signature header format.

Please note that this is a header which includes a list of the headers in it as well. format:

The Header: X-Ca-Signature stringToSign=HTTPMethod+"\n"+ Headers+"\n"+API_URI+"\n"+FormattedQFStr;

which I believe translates to something like

GET
X-Ca-Key:ACCESSKEY
X-Ca_Nonce:UUID
X-Ca-Timestamp:UNIX_TIMESTAMP
api/open/v1/device/checkMac
mac=MACADDR

I have tried the three following methods of formulating the header

stringToSign = str("GET" + "n\\" + "X-Ca-Key:" + ACCESSID + "n\\" + "X-Ca_Nonce:" + UUID + "n\\" + "X-Ca-Timestamp:" + MILLI_TIMESTAMP_STRIPPED + "n\\" + "api/open/v1/device/checkMac" + "n\\" + "mac=MACADDR")

stringToSign = str("GET" + os.linesep + "X-Ca-Key:" + ACCESSID + os.linesep + "X-Ca_Nonce:" + UUID + os.linesep + "X-Ca-Timestamp:" + MILLI_TIMESTAMP_STRIPPED + os.linesep + "api/open/v1/device/checkMac" + os.linesep + "mac=MACADDR")

stringToSign = str("GET" + "\n" + "X-Ca-Key:" + ACCESSID + "\n" + "X-Ca_Nonce:" + UUID + "\n" + "X-Ca-Timestamp:" + MILLI_TIMESTAMP_STRIPPED + "\n" + "api/open/v1/device/checkMac" + "\n" + "mac=MACADDR")

The last two appear to give me the correct format:

GET
X-Ca-Key:ACCESSKEY
X-Ca_Nonce:UUID
X-Ca-Timestamp:UNIX_TIMESTAMP
api/open/v1/device/checkMac
mac=MACADDR

but on my request

payload = {'mac': MACADDR }
HEADERS = {"X-Ca-Key": ACCESSID, "X-Ca-Timestamp" : MILLI_TIMESTAMP_STRIPPED, "X-Ca-Nonce": UUID, "X-Ca-Signature": stringToSign,}
response = requests.get(URL_ARRAY[3], headers=HEADERS, params=payload)

Requests does not like the linebreaks at all and gives me requests.exceptions.InvalidHeader: Invalid return character or leading space in header: X-Ca-Signature.

Any advice to steer me in the right direction is very much appreciated.

0 Answers
Related