wit-ai / wit

Natural Language Interface for apps and devices
https://wit.ai/
931 stars 91 forks source link

Import agent API gives error: {"error":"Something went wrong. We've been notified","code":"unknown"}` #2660

Closed umnibot closed 8 months ago

umnibot commented 11 months ago

What is the current behavior? I am trying to import an agent via the API using this cURL.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.wit.ai/import?v=20230215&name=newapp&private=true');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer THE_TOKEN',
    'Content-Type: application/zip',

]);

curl_setopt($ch, CURLOPT_POSTFIELDS, 'https://url-to-file/exported-agent.zip');
$response = curl_exec($ch);

curl_close($ch);

But I am getting this error: {"error":"Something went wrong. We've been notified","code":"unknown"}

The token I am using is correct as I can create a new app with it if not importing an agent.

umnibot commented 11 months ago

Any help here?

umnibot commented 10 months ago

After 3 weeks of tests, I found a solution and successfully uploaded an existing agent/backup. The thing was to set the body to binary. To set the body to binary data and upload without multipart/form-data, the key is to cheat curl, first, we tell him to PUT, then to POST:

This is the code that worked

$url = 'https://api.wit.ai/import?v=20230215&name=newapp&private=true';
$file_local_full = '/path-to-file/exported-agent.zip';
$content_type = mime_content_type($file_local_full);

$headers = array(
"Content-Type: $content_type", // or whatever you want
"Authorization: Bearer THE_TOKEN"
);

$filesize = filesize($file_local_full);
$stream = fopen($file_local_full, 'r');

$curl_opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PUT => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_INFILE => $stream,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1
);

$curl = curl_init();
curl_setopt_array($curl, $curl_opts);

echo $response = curl_exec($curl);

fclose($stream);

Credits to Sergio A. Kessler https://stackoverflow.com/questions/31970541/php-curl-how-to-set-body-to-binary-data