0

Does any here have experience with Knack API(https://www.knack.com/) here?

I'm using PHP curl to post an image.

When I use POSTMAN, the image successfully uploads. But when I copy the code from POSTMAN(postman option to copy the source code) and add it to my source code, knack returns empty object error("Please select a file to upload").

Knack API documentation: https://www.knack.com/developer-documentation/#file-image-uploads

The POSTMAN source code below

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.knack.com/v1/applications/app-id/assets/file/upload",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"C:\\Users\\shavk\\Pictures\\292937.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: application/x-www-form-urlencoded",
    "Postman-Token: fe1f4eea-b2b5-2d43-2c3c-c262438866d4",
    "X-Knack-Application-Id: id",
    "X-Knack-REST-API-Key: key",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

Any help is highly appreciated.

Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
Shavkat
  • 27
  • 8
  • It doesn’t look like what gets send as CURLOPT_POSTFIELDS does contain any actual file content, it seems to have the file name only …? Anyway, trying to handle the boundary stuff etc. “manually” is not the right way to go about this to begin with, IMHO. My suggestion would be, go read up on how a file upload using PHP & cURL works, instead of just copy&pasting code out of some tool. – 04FS Feb 18 '19 at 10:00
  • [Try this answer to upload file from cURL in PHP](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – Nguyên Ngô Duy Feb 18 '19 at 10:02
  • 04FS and Ngyen thanks for your response. The answer below resolved the issue. – Shavkat Feb 19 '19 at 03:59

1 Answers1

1

Postman's PHP/curl code generator isn't very good. to upload files in the multipart/form-data-format, use CURLFile.

the code should be

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.knack.com/v1/applications/app-id/assets/file/upload",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'files' => new CURLFile("C:\\Users\\shavk\\Pictures\\292937.jpg")
    ),
    CURLOPT_HTTPHEADER => array(
        "X-Knack-Application-Id: id",
        "X-Knack-REST-API-Key: key",
    ),
));

$response = curl_exec($curl);
  • if you are a customer of Postman (using the pro/whatever version), i suggest you make a complaint to customer support about the terrible code generated by the PHP code generator.
hanshenrik
  • 19,904
  • 4
  • 43
  • 89