0

I wrote an application in python to basically post some expenses to a service using its API. Since their API doesn't support multipart/form-data posts in python using the requests library (a pain in the ass), I had to write the post using cURL. My post data has a key with values that are numbers with decimal points. After the post is made, I see that the value of this key gets posted without the decimal point, which leads to a wrong expense post! How can I solve this? Here's the relevant code snippet:

#the data:

PARAMS = {
             "total_cost": netto(), #this is the function that returns the cost as float
             "created_at": date,
             "spent_date": date,
             "user": user,
             "units": int(1)}

#the curl post with file:

file_path = "./path/myfile.pdf"

command = ["curl", "--location", "--request", "POST", url]
for key, value in headers.items():
   command += ["--header", f"{key}: {value}"]
for key, value in PARAMS.items():
   command += ["--form", f"{key}={value}"]
command += ["--form", f"receipt=@{file_path}"]

#calling the curl command:

response = subprocess.run(command, stdout=subprocess.PIPE)

So basically after the post is done, the "total_cost" value is posted without the decimal points. Why??

I don't know how to solve this and have never seen this issue.

MTMP491
  • 3
  • 2
  • "Since their API doesn't support multipart/form-data posts in python using the requests library (a pain in the ass), I had to write the post using cURL" `<--` that doesn't make any sense. Curl when using `--form` is just sending `multipart/form-data` content, and you can [do the same thing in Python](https://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python). – larsks Jan 05 '23 at 13:48
  • i've tried in many ways, it always yielded a 500 error. – MTMP491 Jan 05 '23 at 14:24

0 Answers0