Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloading ReleaseAsset zip File - NullReferenceException #854

Closed
kdolan opened this issue Jul 29, 2015 · 11 comments
Closed

Downloading ReleaseAsset zip File - NullReferenceException #854

kdolan opened this issue Jul 29, 2015 · 11 comments
Assignees

Comments

@kdolan
Copy link

kdolan commented Jul 29, 2015

I am trying to download a zip file that is an asset in a release.

var request = client.Release.GetAll("username", "repo");
var releases = await request;
var latest = releases[0];
var assets = await client.Release.GetAllAssets("username", "repo", latest.Id);
var myAsset_zipFile = assets[0];

var resp= await client.Connection.Get<byte[]>(new Uri(myAsset_zipFile.Url),
          new Dictionary<string, string>(),
          null); //NullReference Here

var data = resp.Body;
var responseData = resp.HttpResponse.Body;
System.IO.File.WriteAllBytes("myZip.zip", (byte[])responseData);

I am getting a NullReferenceException in SimpleJson.cs in the DeserializeObject function at the line that reads obj = ConstructorCachetype; when I call client.Connection.Get.

If I have trying to get this to work without success for some time and have tried several different methods.

It is worth mentioning that I am trying to download from a private repo.

Potentially Related #792

@shiftkey
Copy link
Member

There's definitely something unclear about this API - you can see a test I've written to download an asset here:

https://github.com/octokit/octokit.net/blob/master/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs#L186-L206

In this case, I've done the download slightly different:

var response = await client.Connection.Get<object>(new Uri(asset.Url), new Dictionary<string, string>(), "application/octet-stream");

I think something weird is happening with the deserialization if you don't specify the appropriate Accepts header (probably defaulting to JSON, which would explain something null-ing out.

@kdolan
Copy link
Author

kdolan commented Aug 1, 2015

Unsupported "application/octet-stream"

{Octokit.ApiException: Unsupported 'Accept' header: ["application/octet-stream"]. Must accept 'application/json'.
at Octokit.Connection.HandleErrors(IResponse response) in octokit.net\Octokit\Http\Connection.cs:line 550}

@shiftkey
Copy link
Member

shiftkey commented Aug 2, 2015

@kdolan well that's not great!

Is this a repository I can test out (even privately?) - perhaps there's some edge case here I haven't seen before while working on this area...

@kdolan
Copy link
Author

kdolan commented Aug 2, 2015

@shiftkey I just made a public repo for what I am trying to do. Basically an auto updater that downloads a zip file and then extracts it. In the repo I used the Nuget package but I've also used the project source with the same result.

https://github.com/kdolan/AutoUpdater

@shiftkey
Copy link
Member

shiftkey commented Aug 2, 2015

@kdolan thanks, I'll check it out

@kdolan
Copy link
Author

kdolan commented Aug 5, 2015

@shiftkey After some more trouble shooting this appears to be an issue with the API/request. The error above is from the API not from this codebase. I verified this using postman. It would appear then that maybe the BrowserDownloadUrl is what needs to be used, however, whenever I try to download the file using postman I get a 404 (because it is a private repo) and token authentication does not appear to work for the browser download URL.

cap1
cap2

@shiftkey
Copy link
Member

shiftkey commented Aug 5, 2015

I'm pretty sure that /:user/:repo/releases/:id needs to be called with Accept: application/json as this is returning the payload of release assets:

https://developer.github.com/v3/repos/releases/#get-a-single-release

It would appear then that maybe the BrowserDownloadUrl is what needs to be used

Correct

however, whenever I try to download the file using postman I get a 404 (because it is a private repo) and token authentication does not appear to work for the browser download URL.

Okay, this definitely sounds interesting. Do you see the Authorization header being set when you use the BrowserDownloadUrl value?

@kdolan
Copy link
Author

kdolan commented Aug 6, 2015

var request = client.Release.GetAll("kdolan", "AutoUpdater");
var releases = await request;
var latest = releases[0];
var latestAsset = await client.Release.GetAllAssets("kdolan", "AutoUpdater", latest.Id);

//Download Release.zip here
var response = await client.Connection.Get<object>(new Uri(latestAsset[0].Url), new Dictionary<string, string>(), "application/octet-stream");

byte[] bytes = Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString()); ;
File.WriteAllBytes("Release.zip", bytes);

This code is working for me; I was targeting the wrong API Endpoint. However, the actual writing of the zip file to disk does not seem to always work. The release for AutoUpdater just has a small text file in it and this code works fine, however, when I try other repos with larger Release Zips then the Zip file written is corrupt/incomplete.

@shiftkey shiftkey self-assigned this Aug 25, 2015
@naveensrinivasan
Copy link

I figure out the reason for this It is because content-type is application/octet-stream on the response and the code does not handle that. It handles image,zip,x-gzip.

if (contentType != null && (contentType.StartsWith("image/")

Because it is binary type and we read as string in the else block that is what is causing the corruption. And that is why it does not fail for text files.

I tried including that and it works. I am able to download the release and the file isn't corrupted.

@shiftkey let me know if you want me to do PR on this?

@shiftkey
Copy link
Member

@naveensrinivasan

let me know if you want me to do PR on this?

That'd be great!

@naveensrinivasan
Copy link

👍

naveensrinivasan pushed a commit to naveensrinivasan/octokit.net that referenced this issue Oct 16, 2015
This commit  addressed the `BuildResponse`  wasn't handling
response `content-type` `application/octet-stream` for binary items.
naveensrinivasan pushed a commit to naveensrinivasan/octokit.net that referenced this issue Nov 4, 2015
This commit  addressed the `BuildResponse`  wasn't handling
response `content-type` `application/octet-stream` for binary items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants