passing a csv through an API

Question:

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)

Asked By: OctaveParango

||

Answers:

The issue is more like the string encoding issue.

Looks like the data is being encoded in a url-encoded format. You can use the urllib.parse library to parse the string and extract the data.

Try this:

import urllib.parse

def main(event: dict, context) -> dict:
  body = urllib.parse.parse_qs(event["body"]) // decode the encoded string, instead of base64
  print(body) 

Answered By: tim

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.

Answered By: OctaveParango
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.