1

Almost two days now, trying to upload a *.gzip (or *.gz) file to jet api. Below code uploads the file, but jet api says "Error parsing file: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."

function getCurlValue($filename, $contentType, $postname){
   // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
   // See: https://wiki.php.net/rfc/curl-file-upload
   if (function_exists('curl_file_create')) {
       return curl_file_create($filename, $contentType, $postname);
   }

   // Use the old style if using an older version of PHP
   $value = "@{$filename};filename=" . $postname;
   if ($contentType) {
       $value .= ';type=' . $contentType;
   }

   return $value;
}

function curl_upload_test($file, $remote_url){

$filename = 'gzip-test.gz';

$cfile = getCurlValue($filename, 'application/x-gzip', $filename);


$data = array('file' => $cfile, 'method' => 'PUT');

$ch = curl_init();
$options = array(   CURLOPT_URL => $remote_url,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_CUSTOMREQUEST => $data['method'],
                    CURLOPT_HTTPHEADER => array(
                                            'Accept:',
                                            'Expect:',
                                            'Content-Type: application/x-gzip',
                                            'x-ms-blob-type: blockblob'
                                        ),
                    CURLINFO_HEADER_OUT => true, //Request header
                    CURLOPT_HEADER => 0, //Return header
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_POST => true,
                    CURLOPT_POSTFIELDS => $data
            );

curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return $httpcode;
}

I wen't through their documentation, and found jet api sample code for *.gzip file upload with C#.

    ///Url returned by GET /api/files/uploadToken 
    var url = "https://imupload.blob.core.windows.net/merchant-files/b21611714c8d4734a7686e86f7a8080d?sv=2014-02-14&sr=b&sig=05ssdgrshow383JVEmqk%2BDwrTlHqfEO16gApw%2BgK6%2F2k%3D&se=2014-12-02T17%3A13%3A16Z&sp=w";

   var request = WebRequest.CreateHttp(url);
   request.Method = "PUT";  
   request.Headers.Add("x-ms-blob-type:BlockBlob");

   using (var stream = File.OpenRead(@"C:\temp\test.json"))
   using(var zipped = new GZipStream(request.GetRequestStream(), CompressionMode.Compress))
   stream.CopyTo(zipped);

   request.GetResponse();   

Any help on this highly appreciated.

Shafraz
  • 13
  • 3
  • Are you completely sure that gzip-test.gz is indeed a gzip archive? Judging by the error you're getting, the endpoint API doesn't think so. – Sergey Vidusov Feb 18 '16 at 23:48
  • @Sergey yeah 100% sure the file is a *.gzip archive. Because when i try same thing with the same file via a POSTMAN all works fine. Once i use php app endpoint doesn't recognize it as a *.gzip file. – Shafraz Feb 19 '16 at 02:09

1 Answers1

0

I think you should try and use the actual PUT method instead of POST (see examples here). Also, I think your $data (which you pass into CURLOPT_POSTFIELDS) must contain the actual binary data of your GZIP file:

$data = file_get_contents($filename);

instead of an odd-looking array.

If that doesn't help, a link to the API documentation you're using would be very much appreciated.

Community
  • 1
  • 1
Sergey Vidusov
  • 1,342
  • 1
  • 7
  • 10