There has been other questions on the subject, but nothing seems working for me.
I have a functional curl , but I want to translate to JS (with Node).

curl

curl --user "uername:pass" --digest \
 --header "Content-Type: application/json" \
 --include \
 --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/MY_GROUP/clusters/MY_CLUSTER/fts/indexes?pretty=true" \
 --data '{
     "collectionName": "collname",
     "database": "myDB",
     "mappings": {
       "dynamic": true
     },
     "name": "default"
   }'

响应

HTTP/2 401 www-Authenticate:Digest Realm="MMS Public API", domain="",NONCE="OtwBmcu89QuyVMLW6FP1W/ZjD3preQl0",算法rithm=MD5, QoP="auth",stale=false content-type:application/json Content-Length: 106 x-特使-upstream -服务时间:2日期:星期三,2022年2月16日17:05:37 GMT服务器:特使

HTTP/2 200日期:星期三,2022年2月16日17:05:37格林尼治标准时间 严格传输安全:max-age=31536000;包含子域; 推荐人-策略:跨地域时严格原产地 x-允许跨域策略:无x-内容类型-选项: nosniff x-mongoDB-service-version: gitHash=8284a4ea05955cb13c38df5489a3794b9a691d4f; versionString=v20220216内容类型:application/json X-Frame-Options:拒绝内容-长度:216 X-特使-upstream -服务-时间:154服务器:特使

我不知道为什么会有401和200,但它可以用.然而,我得到的是没有--digest标志的401,下面的解决方案似乎都没有包含摘要?

下面是我try 过的所有方法,它们都返回401"您无权使用此资源":

获取

fetch(url,
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization:
        'Basic ' +
        Buffer.from(
          'user:pass'
        ).toString('base64'),
    },
    body: dataString,
  }
)

与以下各项的结果相同

请求

const headers = {
  'Content-Type': 'application/json',
};

var options = {
  url: url,
  method: 'POST',
  headers: headers,
  body: dataString,
  auth: {
    user: 'user',
    pass: 'pass',
  },
};

request(options, (error: any, response: any, body: any) => {
    ...
});

node-libcurl

  const curl = new Curl();
  curl.setOpt(Curl.option.HTTPHEADER, [
    'Content-Type: application/json',
  ]);
  curl.setOpt(Curl.option.URL, url);
  curl.setOpt(Curl.option.POST, true);
  curl.setOpt(Curl.option.USERPWD, 'user:pass');
  curl.setOpt(Curl.option.POSTFIELDS, dataString);

甚至

PHP

$ch = curl_init();
curl_setopt($ch, curl OPT_URL, $url);
curl_setopt($ch, curl OPT_RETURNTRANSFER, 1);
curl_setopt($ch, curl OPT_POST, 1);
curl_setopt($ch, curl OPT_POSTFIELDS, "...");
curl_setopt($ch, curl OPT_USERPWD, 'user:pass');
curl_setopt($ch, curl OPT_HTTPHEADER, ['Content-Type: application/json']);

$result = curl_exec($ch);
curl_close($ch);

Edit for the package 100

digest请求
  .requestAsync({
    host: url,
    path: urlPath,
    port: 80,
    method: 'POST',
    json: true,
    headers: {
      'Content-Type': 'application/json',
    },
    body: body,
  })
  .then(function (response: any) {
    console.log(response);
  })
  .catch(function (error: any) {
    console.log(error);
  });

响应

错误:请求错误,答案为空

推荐答案

PHP

您需要指定它是摘要:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);

Node Fetch

这是我发现的here个:

fetch(url, {
    ... 
    headers: {
        'Authorization': 'Basic ' + Buffer.from(`${username}:${password}`, 'binary').toString('base64')
    }
    ...
})

将您的代码与上面提议的代码进行比较,我发现您的代码中缺少, 'binary'个.

request-digest

有一个request-digest美元的套餐可能会对你有所帮助.示例(摘自本段中共享的链接):

回拨

var digestRequest = require('request-digest')('username', 'password');
digestRequest.request({
  host: 'http://test.com',
  path: '/api/v1/test.json',
  port: 80,
  method: 'GET',
  headers: {
    'Custom-Header': 'OneValue',
    'Other-Custom-Header': 'OtherValue'
  }
}, function (error, response, body) {
  if (error) {
    throw error;
  }
 
  console.log(body);
});

只promise

var digestRequest = require('request-digest')('username', 'password');
digestRequest.requestAsync({
  host: 'http://test.com',
  path: '/api/v1/test.json',
  port: 80,
  method: 'GET',
  excludePort: false,
  headers: {
    'Custom-Header': 'OneValue',
    'Other-Custom-Header': 'OtherValue'
  }
})
.then(function (response) {
  console.log(response.body);
})
.catch(function (error) {
  console.log(error.statusCode);
  console.log(error.body);
});

Javascript相关问答推荐

Python类实现的JavaScript吊坠是什么样子的?

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

无法将nPM simplex-noise包导入在JS项目中工作

按钮未放置在html dis位置

React 17与React 18中的不同setState行为

使搜索栏更改语言

为什么我的列表直到下一次提交才更新值/onChange

Angular material 拖放堆叠的牌副,悬停时自动展开&

单击ImageListItemBar的IconButton后,在Material—UI对话框中显示图像

如何从调整大小/zoom 的SVG路径定义新的d属性?""

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

无法从NextJS组件传递函数作为参数'

为什么123[';toString';].long返回1?

如何在JAVASCRIPT中合并两组对象并返回一些键

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

按下单键和多值

在验证和提交表单后使用useNavigate()进行react 重定向,使用带有加载器和操作的路由

是否可以将异步调用与useState(UnctionName)一起使用

在JavaScript中,有没有一种方法可以迭代字符串的词法标记?

我在哪里添加过滤器值到这个函数?