TypeError: Request path contains unescaped characters NodeJS + Dynamics

TypeError: Request path contains unescaped characters NodeJS + Dynamics

我正在为 Dynamics CRM 创建 https.request 以获取节点 js 中联系人的数据。

下面是我的代码:

var options = { path: '/api/data/v8.2/contacts?$select=address1_city,address1_line1,address1_line2,address1_line3,contactid,emailaddress1,firstname,fullname,middlename,mobilephone,statecode,suffix,telephone1,vcm_npiid,vcm_prescriberspeciality,vcm_prescriberstatus,vcm_recordtype,vcm_symphonyid&$orderby=fullname asc&$filter=statecode eq 0',
  host: 'xxxxxx.crm.dynamics.com',
  method: 'GET',
  headers:
   { Authorization: 'Bearer xxxxxxx',
     Accept: 'application/json',
     'Content-Type': 'application/json; charset=utf-8',
     Prefer: 'odata.includeannotations=OData.Community.Display.V1.FormattedValue',
     'OData-MaxVersion': '4.0',
     'OData-Version': '4.0' 
   } 
} 

var crmrequest = https.request(options, function(response) { ... }

我收到这个错误:

TypeError: Request path contains unescaped characters

当我尝试在 asc 和 eq 0 查询之间不使用 space 时[通过删除它]。有用。 任何解决方法

您必须使用 querystring.stringify or encodeURI 才能转义特殊字符。

const querystring = require('querystring');
const path = '/api/data/v8.2/contacts';
const qs = {
    $select: 'address1_city,address1_line1,address1_line2,address1_line3,contactid,emailaddress1,firstname,fullname,middlename,mobilephone,statecode,suffix,telephone1,vcm_npiid,vcm_prescriberspeciality,vcm_prescriberstatus,vcm_recordtype,vcm_symphonyid',
    $orderby: 'fullname asc',
    $filter: 'statecode eq 0'
}

const options = { 
  path: path + '?' + querystring.stringify(qs),
  host: 'xxxxxx.crm.dynamics.com',
  method: 'GET'
  /* ... */
}

const path = '/api/data/v8.2/contacts';
const query ='$select=address1_city,address1_line1,address1_line2,address1_line3,contactid,emailaddress1,firstname,fullname,middlename,mobilephone,statecode,suffix,telephone1,vcm_npiid,vcm_prescriberspeciality,vcm_prescriberstatus,vcm_recordtype,vcm_symphonyid&$orderby=fullname asc&$filter=statecode eq 0';


const options = { 
  path: path + '?' + encodeURI(query),
  host: 'xxxxxx.crm.dynamics.com',
  method: 'GET'
  /* ... */
}