Indeed, for a SAM to have a header you need to open it with 'wh'. The 'h' is unnecessary for BAM files (and will lead to an exception if passed).
There must be something else going on, since BAM files always have a header.
Perhaps your downstream tool is assuming a SAM file, tries to read the BAM file, and fails, since obviously there is no valid SAM header in there?