Php NodeJs http请求发送xml格式的api密钥

Php NodeJs http请求发送xml格式的api密钥,php,node.js,xmlhttprequest,Php,Node.js,Xmlhttprequest,这是代码的原始PHP版本,示例=> $curl = curl_init(); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); /////////////////////////////////////////////////////////////////// /// login

这是代码的原始PHP版本,示例=>

$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

///////////////////////////////////////////////////////////////////
/// login
$request='<?xml version="1.0" encoding="UTF-8" ?>
            <Params>
                <ApiKey>abc123</ApiKey>
            </Params>';
            
curl_setopt($curl, CURLOPT_URL, "https://api.unas.eu/shop/login");
curl_setopt($curl, CURLOPT_POSTFIELDS,$request);
$response = curl_exec($curl);
$xml=simplexml_load_string($response);
$token=(string)$xml->Token;
你能帮我弄清楚为什么它不起作用吗?
提前感谢。

主机名不应包含路径:

以下是修复方法:

const options = {
    protocol:'https:',
    host: "api.unas.eu",
    path: "/shop/login",
    method: "POST",
    headers:{
        'Content-Type': 'application/xml',
        'Content-Length': Buffer.byteLength(postData)
    }
};
另一个问题:您没有正确传递post数据:

const req =...

req.write(postData);//send post data to the request

req.on('error', (e) => {
    console.error(e);
});
req.end();

非常感谢,它修复了它。但现在我得到了一个答案错误代码为“500”。如果我修改这个选项:“CURLOPT_POSTFIELDS”:Buffer.byteLength(postData)”,那么我将得到400个错误代码。知道怎么修吗?@Denes:检查我的更新非常感谢。它像我想要的那样工作。祝您有个美好的一天。
const options = {
    protocol:'https:',
    host: "api.unas.eu",
    path: "/shop/login",
    method: "POST",
    headers:{
        'Content-Type': 'application/xml',
        'Content-Length': Buffer.byteLength(postData)
    }
};
const req =...

req.write(postData);//send post data to the request

req.on('error', (e) => {
    console.error(e);
});
req.end();