Gerard Taub
Gerard Taub

Reputation: 177

aws lambda giving "Unable to deserialize request: Unexpected end-of-input: expected close marker for ARRAY" when connecting to neo4j

i am trying to connect to my neo4j db in lambda function. but keeps getting the "Unable to deserialize request: Unexpected end-of-input: expected close marker for ARRAY" error. but all seems fine when sending requesting in webapp using same parameters. Here's my request body:

  var request = {"statements": 
    [
        {
        "statement": "MATCH (p:COMPANY {id: " + event.ID + "})<-[:MADE_BY]-(FRANCHISE) RETURN FRANCHISE"
        }
    ]
  };
  var options = {
      host: hostname,
      path: pathname,
      method: 'POST',
      port: portnumber,
      headers: {
          "Authorization": authInfo,
          "Content-Type": "application/json",
          'Content-Length': Buffer.byteLength(request)
      },
  };
  console.log(JSON.stringify(request));
  var req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
          console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
          console.log('No more data in response.');
      });
  });

  req.on('error', (e) => {
      console.log(`problem with request: ${e.message}`);
  });

  req.write(JSON.stringify(request));
  req.end();

also tried print out the JSON.stringify(request) result but it seems ok to me.

Upvotes: 0

Views: 383

Answers (1)

cybersam
cybersam

Reputation: 67044

When you call Buffer.byteLength(request), you are passing the request object itself instead of the JSON-stringified form of that object. This causes the Content-Length header value to be too small.

Try this instead:

  var request = {"statements": [ {
        "statement": "MATCH (p:COMPANY {id: {event_id} })<-[:MADE_BY]-(FRANCHISE) RETURN FRANCHISE",
        "parameters": {
          "event_id": event.ID
        }
    } ]
  };
  var request_str = JSON.stringify(request);
  var options = {
      host: hostname,
      path: pathname,
      method: 'POST',
      port: portnumber,
      headers: {
          "Authorization": authInfo,
          "Content-Type": "application/json",
          'Content-Length': Buffer.byteLength(request_str)
      },
  };
  console.log(request_str);
  var req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
          console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
          console.log('No more data in response.');
      });
  });

  req.on('error', (e) => {
      console.log(`problem with request: ${e.message}`);
  });

  req.write(request_str);
  req.end();

Notice also that this code passes event.ID as a parameter, which is more efficient if this code will be called repeatedly.

Upvotes: 1

Related Questions