Roy Holzem
Roy Holzem

Reputation: 870

cURL send JSON and PDF file

I have this cURL command to speak with my API:

curl -i -H "Content-Type: application/json" -X POST -d '{"sid":"01234567","text":"TEST","tlf":"123456","ctc":"123456","opr":"XYZ","fbm":"[email protected]","flag":"XYZ","product":"XYZ"}' http://XXX.XXX.XXX.XXX:8080/todo/api/v1.0/tasks

Which works perfectly, here the response:

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 24
Server: Werkzeug/0.11.3 Python/2.6.6
Date: Wed, 07 Jun 2017 10:03:00 GMT

Now I want to send a pdf file in that same command.

I figured I add:

"file":"/var/www/html/olotool/API/attachments/pdf-test.pdf"

at the end but it gets saved as a string instead of a document.

How can I add this in one command?

UPDATE:

"file":"$(base64 ~/attachments/pdf-test.pdf)"

Changes the outcome into <type 'file'> it's still a string but at least it's content is not (base64 ~/attachments/pdf-test.pdf).

Upvotes: 1

Views: 7225

Answers (2)

user14551062
user14551062

Reputation: 1

curl -X GET --header "Accept: application/json" "https://wsmipres.sispro.gov.co/WSMIPRESNOPBS/api/PrescripcionPaciente/900204617/2020-06-19/C68A900A-9FB9-46CB-B24B-4AB56E078600/CC/1098798857"

Upvotes: -2

Roy Holzem
Roy Holzem

Reputation: 870

Here is the answer:

curl -i -F data='{"sid":"0123456","text":"TEST","tlf":"13245679","ctc":"123456","opr":"XXX","fbm":"[email protected]","flag":"XYZ","product":"XYZ"}' -F 'file=@/var/www/html/olotool/API/testpdf/test.pdf' http://XXX.XXX.XXX.XXX:8080/todo/api/v1.0/tasks

On the API instead of using request.json you use request.form then convert datato json with json.loads(data).

For the file, you need to use request.file['files'] and dont forget to encode files like pdf's or word docs to base64 if you want to import them to MySQL with file.read().encode("base64")

Upvotes: 6

Related Questions