简体   繁体   中英

curl send the file to api

I'm having trouble sending a file to api via curl. I have a code that was generated in postman but unfortunately it doesn't work. I got the libraries for postman from the producer.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.accept.autenti.net/api/v0.1/documents/*********************/files",
  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('files'=> new CURLFILE('/path/to/file'),'files'=> new CURLFILE('/path/to/file')),
  CURLOPT_HTTPHEADER => array(
    "Authorization: *********************",
    "Content-Type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

server response:

{"error":{"id":"8fca0a50-8e8f-48e7-9855-30d27fdd45fd","key":"NO_FILES_GIVEN"}}

I modified it according to the documentation. I have added to CURLOPT_HTTPHEADER

"Content-Type: multipart / form-data; boundary = Content-Disposition: form-data; name = 2665; Content-Type: application / pdf",
"Content-Length: 192897"

And at the moment it looks like this:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.accept.autenti.net/api/v0.1/documents/*********************/files",
  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('files'=> new CURLFILE('2665.pdf')),
  CURLOPT_HTTPHEADER => array(
    "Authorization: *********************",
    "Content-Type: multipart/form-data; boundary=Content-Disposition: form-data; name=2665; Content-Type: application/pdf",
    "Content-Length: 192897"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

server response:

{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}

Here is the link to the documentation : https://autenti.com/api/ Upload.

that's a bug in postman, the generated code makes no sense, you should send a bugreport to postman, if you can be arsed.

    CURLOPT_POSTFIELDS => array(
        'files' => new CURLFILE('/path/to/file'),
        'files' => new CURLFILE('/path/to/file')
    )

is barely legal, and nonsensical php code, perhaps it was supposed to be

    CURLOPT_POSTFIELDS => array(
        'files' => array(
            0 => new CURLFILE('/path/to/file'),
            1 => new CURLFILE('/path/to/file')
        )
    )

also if it's indeed a multipart/form-data -post, this header is just wrong , a bug in the generator most likely: "Content-Type: application/x-www-form-urlencoded"

and do not set the "Content-Type: multipart / form-data; boundary -header manually, instead let curl generate it for you automatically, also do not set the "Content-Length: 192897" header manually, it's VERY likely to be incorrect, as the length depends on the file size AND the length of the boundary AND even the filename of the file you're uploading, and curl will generate the header for you automatically if you don't (and curl won't make any mistakes in the length headers, unlike you)

the

{"error":{"id":"c7a19220-f953-4ffd-893b-18914bbb161d","key":"FILE_SIZE_LIMIT_EXCEEDED"}}

error could mean that you actually exceeded the limit, but it's more likely that your hardcoded Content-Length: 192897 -header has a bogus value, and the bogus content-length header value made the server confused and generate the "FILE_SIZE_LIMIT_EXCEEDED"-error.

but skimming through the documentation, looks to me like it should be:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL=> 'https://api.accept.autenti.net/api/v0.1/documents/*********************/files',
    CURLOPT_HTTPAUTH => CURLAUTH_BEARER,
    CURLOPT_XOAUTH2_BEARER=>"token",
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS=>array(
        new CURLFile($filename, "application/pdf")
    )
));
curl_exec($ch);

... and that their example code is terrible. here is the request generated by the above code:

POST /api/v0.1/documents/*********************/files HTTP/1.1
Host: api.accept.autenti.net
Authorization: Bearer token
Accept: */*
Content-Length: 193
Content-Type: multipart/form-data; boundary=------------------------d058e8488f15fa10

--------------------------d058e8488f15fa10
Content-Disposition: form-data; name="0"; filename="test.file"
Content-Type: application/pdf

lol

--------------------------d058e8488f15fa10--

which looks very close to what they want in the sample code...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM