axios nodejs“错误:请求失败,状态码为 400”

时间:2021-05-03 01:47:35

标签: node.js curl axios

我是 nodejs 的初学者,在我使用 php 之前。 我想学习 CURL 请求。但是在节点中它很难(所有节点都很难) 建立库“axios”以提出简单的卷曲请求。 我拿了这个代码

async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

来自 https://github.com/axios/axios (它不起作用,因为我需要“用户”处理程序文件) 我把它改成了

const axios = require('axios');
async function run() {
    try {
        let response = await axios.post("https://api.bitaps.com/btc/v1/create/payment/address", {
            forwarding_address: '1F1tAaz5x1HUXrCNLbtMDqcw6o5GNn4xqX',
            callback_link: 'test1.ru',
            confirmations: 3
        });
        console.log(response.data);
    }
    catch(e) {
        console.log(e);
    }
}
run();

这狗屎,给我抛出错误

Error: Request failed with status code 400
    at createError (H:\xampp\htdocs\exchanger\node_modules\axios\lib\core\createError.js:16:15)
    at settle (H:\xampp\htdocs\exchanger\node_modules\axios\lib\core\settle.js:17:12)
    at IncomingMessage.handleStreamEnd (H:\xampp\htdocs\exchanger\node_modules\axios\lib\adapters\http.js:260:11)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) 

WHYYYYYY?来自 axios 文档的官方示例。

但在 PHP 中,这成功运行,没有任何 400 状态代码和错误。


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.bitaps.com/btc/v1/create/payment/address",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"forwarding_address\":\"18Hh2hEY8cV4mk9dQwjzyjkEzrUgMY71Kv\",\"callback_link\":\"http://www.2qgsl7gnaiiypmda.onion/handler\",\"confirmations\":2}",
  CURLOPT_HTTPHEADER => array(
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

抱歉我的英语不好。谢谢)

0 个答案:

没有答案