首页 > 解决方案 > NodeJS_RequestLib - How do I set customHeaders?

问题描述

I'm using the request lib request in my project and I need to inform to the API of another company (I can't modify the server) that a transaction was did. Anyway, to inform to this API I need to use a custom header named api_key and when I try to do this, like that:

reqOptions = {
    url: 'https://my_url_here.com',
    body: JSON.stringify(transaction),
    headers: {
        'Content-Type': 'text/xml',
        'charset': 'UTF-8',
        'api_key:': 'my_api_key_here'
    }
};

I'm getting the error:

error: TypeError [ERR_INVALID_HTTP_TOKEN]: Header name must be a valid HTTP token ["api_key:"]

How can I set the custom header?

标签: node.jsrequest

解决方案


Try change:

'api_key:': 'ak_live_a2a8ffae58614a42f6ab67c80a552eb1488e6'

to:

'api_key': 'ak_live_a2a8ffae58614a42f6ab67c80a552eb1488e6'

推荐阅读