mdneuzerling / lambdr

Run R containers on AWS Lambda
https://lambdr.mdneuzerling.com
Other
131 stars 12 forks source link

POST request with FormData #11

Closed fmmattioni closed 2 years ago

fmmattioni commented 2 years ago

Hey @mdneuzerling,

I have been trying to get a function to work that handles a POST request that has a file (FormData) in its body. This is the header that I is passed in the request:

headers: {
 'Content-Type': 'multipart/form-data',
}

What would you recommend me to do in this case? If it helps, I can quickly create a repo as a reprex.

Thanks in advance!

mdneuzerling commented 2 years ago

Hi @fmmattioni. This sounds like an interesting use case! Is lambdr struggling to parse the event body?

fmmattioni commented 2 years ago

Indeed.. the data is passed as binary, would that be an issue for {lambdr}?

mdneuzerling commented 2 years ago

Not a problem! If you use an identity deserialiser, your handler function will receive the raw event body as is. The deserialiser is responsible for converting the event body into something that the handler function can process. Normally it turns JSON into an R list, which is passed to the handler function as arguments. But in this case we'd rather use the identity function to make the deserialiser leave the event body alone. It would then be up to your handler function to deal with the binary data. Example:

start_lambda(
  config = lambda_config(deserialiser = identity)
)

Please let me know if that doesn't work!

fmmattioni commented 2 years ago

OMG this worked like a charm! I guess the issue was that I literally did not know what the identity function was.. thank you so much!