Auth0 Home Blog Docs

Hitting AWS API Gateway endpoint in Hook getting error in response

auth0
hooks
aws
lambda

#1

Hi All,

I’m trying to setup a post registration hook, so once a user registers it hits a lambda function. The lambda function works fine as tested via the AWS console.

I’ve installed the request npm module so I can do a post request to the endpoint, but I keep on getting the following error returned.

Unexpected string in JSON at position 33",

I’ve looked around and found that I need to add my endpoint to the API dashboard but everytime I try I get a Forbidden message.

Any help would be greatly appreciated


#2

Unexpected string in JSON is likely due to the JSON being returned not serialized correctly. It would be good to know where that error is being thrown. Are you parsing JSON as part of your response? Is that error coming from your lambda function? Someone somewhere is trying to parse JSON and whatever is given to it is not JSON. This often happens when you are requesting a content-type of JSON, but what is actually returned from the web service is not JSON (like an HTML error page or something like that).

Once you find where that is being thrown, you might want to print out what the JSON looks like and see if it is an HTML error or something like that that could give you a clue as to what the underlying error really is.


#3

Hi @Carlos_Mostek

thanks for a quick response. This is my code.

const request = require('request');
const apiURL = 'https://xxxxxxxx.execute-api.eu-west-2.amazonaws.com/xxxxxxx';

module.exports = function (user, context, cb) {
  request.post({
    url:apiURL, 
    json: { 
      "userId": user.id, 
      "userEmail": user.email,
      "userEmailVerified": user.emailVerified
    }
  }, (error, response, body) => { 
    cb(error, response);
  });
};

As you can see I’m posting a JSON via the request post and the error seems to be coming from Auth0 not my Lambda function as thats not even getting hit.

This is a more in depth error message from the Extend editor runner. The line its complaining about (40), doesn’t even exist so not sure what to make of it.

"SyntaxError: Unexpected string in JSON at position 40    
    at JSON.parse (<anonymous>)    
    at Object.internals.tryParseBuffer (/data/sandbox/node_modules/auth0-ext- 
    compilers/node_modules/wreck/lib/index.js:536:27)
    at finish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:368:28)
    at wrapped (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/hoek/lib/index.js:879:20)
    at module.exports.internals.Recorder.onReaderFinish (/data/sandbox/node_modules/auth0-ext- 
    compilers/node_modules/wreck/lib/index.js:415:16)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at module.exports.internals.Recorder.emit (events.js:208:7)
    at finishMaybe (_stream_writable.js:614:14)
    at afterWrite (_stream_writable.js:465:3)"
}
ln 26, col 16

Thanks


#4

It isn’t line 40, it is character 40. If this is happening in the hook code, it would be good to be able to see the hook code.

If you install the realtime webtask log extension, then you can add some console.log’s to your hook to track down where you are running into issues in the hook. If you can isolate it down to an individual line, that would be good.

It seems like if you aren’t doing a JSON.parse directly, then what is probably happening is that it is calling some endpoint and the endpoint is not returning valid JSON. With console.logs, hopefully you can isolate down where that is happening.