类型错误:请求路径包含未转义的字符 NodeJS + 动态



我正在向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) { ... }

我收到此错误:

类型错误:请求路径包含未转义的字符

当我尝试在 asc 和 eq 0 查询中没有空格时[通过删除它]。它有效。任何解决方法

您必须使用 querystring.stringify 或 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'
  /* ... */
}

最新更新