adding undefined checks to prevent sending "undefined" in the headers.