Question

I'm using a NodeJS express server as middleware for an iOS app in order to store data in a Sharepoint list.

The iOS app makes a post request with the payload to a NodeJS server, which then authenticates up against Sharepoint 2013 and makes a POST request.

I'm assuming that authentication isn't an issues since I can read all items just fine. But when I try to create a new item in the list i get the following error:

"statusCode": 400,
"body": "{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected.\"}}}"

My payload looks like this:

{
    "__metadata": {
    "type": "SP.Data.ResultsListItem"
    },
    "Vessel" : "TEST"
}

The post request inside my iOS app looks like this (angular.js):

item = {
  "__metadata": {
  "type": "SP.Data.ResultsListItem"
  },
  "Vessel" : $scope.selectedVessel.name,
 };

   $http({
    method: 'POST',
    url: $scope.selectedVessel.endpoint + "/post/survey",
    data: JSON.stringify(item),
   })
   .then(function(response) {
      console.log(response);
    })

And finally the code inside the NodeJS server:

//Endpoint for posting results
app.post('/post/survey', function(req, res) {

//Get Sharepoint context

 httpntlm.post({
    url: siteurl + "/_api/contextinfo",
    username: credentials.username,
    password: credentials.password,
    domain: credentials.domain,
    headers: {
        "Accept": "application/json;odata=verbose"
    }
}, function(err, data) {

    token = JSON.parse(data.body);

    token = token.d.GetContextWebInformation.FormDigestValue;

    createItem(token);
})


var postBody = req.body;
console.log(postBody);
var webUrl = "https://sp2016.xxx.com";



function createItem(token){

httpntlm.post({
    url: webUrl + "/_api/lists/getbytitle('Results')/items",
    username: credentials.username,
    password: credentials.password,
    domain: credentials.domain,
    body: JSON.stringify(postBody),
    headers: {
         "accept": "application/json;odata=verbose",
         "content-type": "application/json;odata=verbose",
         "X-RequestDigest": token,
         "X-HTTP-Method": "POST",
         "If-Match": "*"
    }
}, function(err, data) {
    if (err) console.log(err);

    res.send("Done")
    console.log(data)
});

}

})

Any suggestions to what is causing the error?

Was it helpful?

Solution

So i found the issue. It seems the payload wasn't parsed properly. So i used body-parser package.

I then applied it on all routes:

var bodyParser = require("body-parser");
app.use(bodyParser.json());

This fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top