0

I am a beginner. I am trying to change a curl cmd to an actually Post request in my python code. Each time, I am getting either a 404 or 400 errors. I am a bit lost. Thanks.

Curl request : (echo -n '{"data": "'; base64 test-1.pdf; echo '", "extension": ".pdf"}') | curl -X POST -H "Content-Type: application/json" -d @- http://localhost:5000

My python code:

import json
import requests
url ='http://localhost:5000/POST'
newHeaders = {'Content-Type': 'application/json'}
response = requests.post(url, json={"data": "'; base64 test-1.pdf; echo '", "extension": ".pdf"},headers=newHeaders)
print("Status code: ", response.status_code)
response_Json = response.json()
print("Printing Post JSON data")
print(response_Json['data'])
print("Content-Type is ", response_Json['headers']['Content-Type'])
7
  • 1
    I think you just want url ='http://localhost:5000' ... not that /POST bit Commented Jul 29, 2020 at 23:03
  • thanks for the quick reply, I am still getting an error, but this time is 500. On the server, the error is more explicit: File "/home/myuser/backend/main.py", line 23, in predict fp.write(base64.b64decode(data)) File "/usr/lib/python3.7/base64.py", line 87, in b64decode return binascii.a2b_base64(s) binascii.Error: Incorrect padding Commented Jul 29, 2020 at 23:08
  • @JulieM 500 is a server-side error. Commented Jul 29, 2020 at 23:09
  • Does your curl command work successfully? Commented Jul 29, 2020 at 23:10
  • the curl is successful Commented Jul 29, 2020 at 23:12

1 Answer 1

3

Your URL is wrong and should not have the /POST at the end, but in addition to that, you need to actually base64-encode the test-1.pdf (this is what the shell command that runs curl is doing).

You could use this (combined with the code in the question) to put the correct value into the parameters dictionary.

import base64

#...
b64 = base64.b64encode(open("test-1.pdf", "rb").read()).decode()
response = requests.post(url,
                         json={"data": b64,
                               "extension": ".pdf"},
                         headers=newHeaders)
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Alaniwi, thank a lot for the quick and detailed reply, I have now another error : TypeError: Object of type bytes is not JSON serializable
@JulieM Edited - please give this a try. I've inserted a decode to turn the bytes into a string. Unfortunately I can't test it as it is relying on whatever server you are running on localhost.
I tried and I have this error : File "<ipython-input-86-7f2d14f02b90>", line 6 responses = requests.post(url, ^ SyntaxError: invalid syntax thank again
@JulieM Sorry, a stray close parentheses - I'll remove it. (Now done.)
I added an extra bracket after encode()) but i have a new error :UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 11: invalid continuation byte
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.