0

I'm trying to upload a config file via PHP's CURL to the Cisco APIC-EM via POST but am receiving an unexpected error:

//File Upload
echo "<b>File Upload:</b><br>";
$namespace = "config";
$data = array(
        "version" => "",
        "response" => 
        array(
            "nameSpace" => $namespace,
            "encrypted" => false,
            "id" => "",
            "md5Checksum" => "",
            "sha1Checksum" => "",
            "fileFormat" => "",
            "fileSize" => "",
            "downloadPath" => "http://hama0942.global.bdfgroup.net/network/net_config/bdfbrsao00000000rt02.txt",
            "name" => "bdfbrsao00000000rt02.txt",
            "attributeInfo" => "object"
            )
        );
$data = json_encode($data);
//For Debugging only - Request
echo "<br>Request:<br>";
echo '<pre>';
print_r($data);
echo '</pre>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://hams1484.global.bdfgroup.net/api/v1/file/".$namespace);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-    data", "X-Auth-Token: ".$serviceTicket));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array($data));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ch);
echo "<br><b>Request-Header: </b>".curl_getinfo($ch, CURLINFO_HEADER_OUT )."<br><br>";
//For Output Debugging only - Response
echo "<br>Response:<br>";
echo '<pre>';
print_r(json_decode($response));
echo '</pre>';
curl_close($ch);
echo "<br><hr>";

This is the result:

File Upload:

Request:

{"version":"","response":{"nameSpace":"config","encrypted":false,"id":"","md5Checksum":"","sha1Checksum":"","fileFormat":"","fileSize":"","downloadPath":"http:\/\/hama0942.global.bdfgroup.net\/network\/net_config\/bdfbrsao00000000rt02.txt","name":"bdfbrsao00000000rt02.txt","attributeInfo":"object"}}


Request-Header: POST /api/v1/file/config HTTP/1.1 Host: hams1484.global.bdfgroup.net Accept: */* X-Auth-Token: ST-1111-OUiNBLtgALg2ufcwFrh5-cas Content-Length: 436 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------5e86cb7113449960


Response:

stdClass Object
(
[response] => stdClass Object
    (
        [errorCode] => 7062
        [message] => Unexpected error
        [detail] => Non valid file object
    )

[version] => 1.0
)

I'm afraid that there is something wrong with the CURL settings but I'm not sure. Has someone an idea what is wrong here? I'm using PHP7.

Thanks.

Jonast92
  • 4,964
  • 1
  • 18
  • 32

1 Answers1

0

I have been struggling with this for a long time now and finally found a solution for this.

1 : File for upload has to be local file on the server your are CURL'ing from.

2 : $file = realpath("filenameOnServer");

3 : $config = new CURLfile($file);

4 : $array = array("fileUpload" => $config); $array = json_encode($array);

5 : curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: multipart/form-data; charset=utf-8;', 'X-Auth-Token: ' . $token,
));

6 : send the array as POST variable in the CURL. Nothing else is needed

MHopstad
  • 333
  • 2
  • 16