
I'm trying to insert a new record into a custom entity with the Web API using Node.js and the "http" object. I don't want to use the client SDK and AJAX because it's in transition. My goal is to use generic JS and JQuery against the Web API.
1) I have been successful using as is it to retrieve records
2) I have been successful using XMLHTTPRequest
3) If I try using the following code I get no errors, no response and the record is not inserted.. nothing happens. but if I use the same code using a GET instead of a POST it works fine.
I'm looking at the http.request content and it looks fine. Any ideas?
function insertRecord(requestheaders, entity, callback) {
var tempRec = {};
...assign values
var dataRec = JSON.stringify(tempRec);
var myhost = 'mytenant.api.crm.dynamics.com';
var mypath = "/api/data/v8.2/awl_reactcustomercampaignstatuses";
//set the crm request parameters. Note that requestheaders contains a valid token
var crmrequestoptions = {
'host': myhost,
'path': mypath,
//'url': myhost+mypath,
'method': 'POST',
'headers': requestheaders,
"Content-Length": Buffer.byteLength(dataRec)
};
//make the web api request
var myrequest = https.request(crmrequestoptions, function(response) {
//make an array to hold the response parts if we get multiple parts
var responseparts = [];
response.setEncoding('utf8');
response.on('data', function(chunk){
//add each response chunk to the responseparts array for later
responseparts.push(chunk);
});
response.on('end', function(){
//once we have all the response parts, concatenate the parts into a single string
var completeresponse = responseparts.join('');
//parse the response JSON
var collection = JSON.parse(completeresponse).value;
if (collection==null){
console.log('No Rows Returned')
}else{
console.log(completeresponse);
};
});
});
myrequest.on('error', function(e) {
console.error(e);
});
myrequest.write(dataRec);
//close the web api request
myrequest.end();
};
here's the request text .....................
{ host: 'mytenant.api.crm.dynamics.com',
path: '/api/data/v8.2/my_customercampaignstatuses',
method: 'POST',
headers:
{ Authorization: 'Bearer XXX......',
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8' },
data: '{"my_PeopleID@odata.bind":"/contacts(<GUID>)","my_CampaignID@odata.bind":"/campaigns(<GUID>)","my_dmapprovalcode":"AP-234","my_emailapprovalcode":"EM-002","my_campaignurl":"www.my.com/CampaignInfoTest","my_campaigntfn":"8001234567","my_offeredamount":210872.33,"my_offeredrate":3.99,"my_offeredterm":360,"my_startdate":"2018-02-28T05:00:00.000Z","my_expiredate":"2018-03-30T04:00:00.000Z","my_mycreated":"2018-02-26T22:56:56.539Z","my_myupdated":"2018-02-26T22:56:56.539Z"}' }
No Rows Returned
*This post is locked for comments
I have the same question (0)