Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware: urlDecodePathParameters #429

Closed
willfarrell opened this issue Dec 3, 2019 · 1 comment
Closed

Middleware: urlDecodePathParameters #429

willfarrell opened this issue Dec 3, 2019 · 1 comment

Comments

@willfarrell
Copy link
Member

willfarrell commented Dec 3, 2019

More for completeness, but ran into a case when updating the API for a legacy app. Can put a PR together if needed.

Edit:

module.exports = () => ({
  before: (handler, next) => {
    if (handler.event.pathParameters) {
      for (let key in handler.event.pathParameters) {
        try {
          handler.event.pathParameters[key] = decodeURIComponent(
            handler.event.pathParameters[key]
          )
        } catch (e) {
          console.error(e)
        }
      }
    }

    next()
  }
})
@lmammino
Copy link
Member

lmammino commented Dec 3, 2019

Sounds good!

one small detail to be aware though is that decodeURIComponent can throw. So we should handle that:

try { 
  var a = decodeURIComponent('%E0%A4%A'); 
} catch(e) { 
  console.error(e); 
}

// URIError: malformed URI sequence

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants