pythoncsvaws-lambda

passing a csv through an API


I'm trying to pass a csv through an api like this:

The csv is as follow:

field_1, field_2, field_3
1, 3, 7

the push code is this:

with open("sample_csv.csv") as f:
    data = {"file": f}
    print(data)
    headers = {
        "Content-Type": "text/csv",
        "File-Type": "POS",
        "File-Key": "somekey",
    }

    r = requests.post(endpoint, data=data, headers=headers)

However when i read it from a Lambda on the other end i get this:

b'file=field_1%2C+field_2%2C+field_3%0A&file=1%2C+3%2C+7'

When i run the above string through chardet it tells me its ascii, but i dont know how to convert it


edit: lambda function code:

def main(event: dict, context) -> dict:
  body = base64.b64decode(event["body"])
  print(body)


Solution

  • Figured it out, adding answer here for posterity

    decoded_event = base64.b64decode(event["body"])
    parsed_event = urllib.parse.parse_qs(x.decode())["file"]
    

    After which parsed_event is a list with each element being a line of the csv which you can then manipulate as needed.