0
votes

Im trying to update a distribution list by sending a put request, when I run this code and test it in postman by giving it a JSON body I get this error in my node.js terminal stating SyntaxError: Unexpected end of JSON input ... any idea what I should change?

My PUT API request

 app.put("/api/Dls/Add/:groupId" , (req, res) => {
    const response = {
      success: false
    };
    if (Authorized.myToken) {
      response.success = true;
      response.data = {};
      var options = {
        method: 'PUT',
        url: 'https://SomeAPI.com/' + req.params.groupId,
        headers:
        {
          Accept: 'application/json',
          Authorization: 'Bearer' + ' ' + Authorized.myToken
        },

        body: JSON.stringify(req.body)

      };
      request(options, function (error, response, body){
        if (error) {
          console.log(error);
          return;
        }
        const data = response.body;
        const dls = JSON.parse(data)
        return res.json(dls);

      });
    }
 }); 

JSON body I'm passing through postman to test the API call

{
        "groupId": "123456789",
        "SomeField1": null,
        "SomeField2": "xxxxxxxxx",
        "SomeField3": true,
        "SomeField4": "xxxxxxxxx",
        "SomeField5": "xxxxxxxxx",
        "SomeField6": [
            "xxxxxxxxx"
        ],
        "SomeField7": "xxxxxxxxx",
        "SomeField8": "xxxxxxxxx",
        "SomeField9": "xxxxxxxxx",
        "SomeField10": "xxxxxxxxx",
        "SomeField11": [],
        "SomeField12": "xxxxxxxxx",
        "SomeField13": null,
        "SomeField14": false,
        "SomeField15": ["xxxxxxxxx"]
}

Any feedback is appreciated!

2

2 Answers

0
votes

If the JSON that you posted here is the real one that you pass via postman then, it is not the valid JSON as you have the same name properties. When I say valid it means you get something like this after posting to the endpoint.

{
  "groupId": "123456789",
  "SomeField": [
    "xxxxxxxxx"
  ]
}

Request npm package is also deprecated so it is better to not use it and replace it with something like Axios. TBH I did not see any error in the code that causes the error that you mentioned, do you have access to the API to check the logs? Maybe something went wrong on the https://SomeAPI.com/ endpoint.

0
votes

I figured out what the issue was, I needed to add the .end to the return statement

ex. return res.status(200).end()