-1

I have this php code that sends a POST request containing a file:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://****/ocr_api/',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('file'=> new CURLFile('/C:/Users/me/Downloads/test.pdf')),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

I copied this code from postman under the "code" options, so this should be the equivalent of it and work without issue? But it still doesnt.

request.files shows an empty dictionary then doesnt make it further than that line

@app.route("/", methods=["POST"])
def accept_pdf():
    print("HERE", request.files) ## SHOWS UP EMPTY AND STOPS HERE
    pdf = request.files["file"]
    filename = pdf.filename
    print("FILENAME", filename)

Any help? Thank you

davidism
  • 121,510
  • 29
  • 395
  • 339
Amon
  • 2,725
  • 5
  • 30
  • 52
  • https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php @Amon check the answer of above question – Dhaval Purohit Mar 11 '23 at 15:35
  • Does this answer your question? [how to upload file using curl with PHP](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – Dhaval Purohit Mar 11 '23 at 15:37

1 Answers1

0

Try to set multipart/form-data in your call:

curl_setopt_array($curl, array(
  [...]
  CURLOPT_HEADERS => true,
  CURLOPT_HTTPHEADER => array("Content-Type:multipart/form-data"),
  [...]
));
  • Hey thank you for the answer, it's still returning the 400 error. On my backend I'm printing off `request.files` and it's returning `ImmutableMultiDict([])` – Amon Mar 06 '23 at 20:51
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '23 at 15:31