Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
How to read the POST parameters in a serverless application with Lambda?

I'm submitting a form to a Lambda function deployed by serverless, here's the yml:

functions:
  hello:
    handler: handler.hello
    events:
      - http: POST hello

Now my hello function is:

module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Se222rverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };

  callback(null, response);
};

I can see on the output that the variables were passed, but they are stored in the event.body property as such:

 "body":"email=test%40test.com&password=test12345"

Now I can access this string, but I can't read individual variables from it, unless I do some regex transformation which, I believe, would not be the case in such a modern stack such as serverless/aws.

What am I missing? How do I read the individual variables?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It looks like your Serverless endpoint is receiving data with Content-Type: application/x-www-form-urlencoded. You could update the request to use JSON data instead to access the post variables the same way you would other JavaScript objects.

Assuming this isn't an option; you could still access your post body data using the node querystring module to parse the body of the request, here is an example:

const querystring = require('querystring');

module.exports.hello = (event, context, callback) => {

  // Parse the post body
  const data = querystring.parse(event.body);

  // Access variables from body
  const email = data.email;

  ...

}

Just remember if some of the parameters in the post body use names that are invalid JavaScript object identifiers to use the square bracket notation, e.g.:

const rawMessage = data['raw-message'];
over 4 years ago · Santiago Trujillo Relatório

0

You can use a the Node querystring module to parse the POST body.

over 4 years ago · Santiago Trujillo Relatório

0

The handler documentation for the various programming models implies that the event type from the API Gateway is a low level stream. That means you have to use other methods to extract the body content from the POST.

Input Format

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

Dotnet Only the System.IO.Stream type is supported as an input parameter by default.

Python event – This parameter is usually of the Python dict type. It can also be list, str, int, float, or NoneType type.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda