richardschneider / net-ipfs-http-client

InterPlanetary File System client for .Net (C#, VB, F# ...)
MIT License
158 stars 52 forks source link

Unhandled exception. System.Net.Http.HttpRequestException: 405 - Method Not Allowed #71

Open fadyanwar opened 2 years ago

fadyanwar commented 2 years ago

Hi, I'm getting the below error using the Nuget package while running an IPFS node locally.

$ dotnet run Unhandled exception. System.Net.Http.HttpRequestException: 405 - Method Not Allowed

at Ipfs.Http.IpfsClient.ThrowOnErrorAsync(HttpResponseMessage response) at Ipfs.Http.IpfsClient.DownloadAsync(String command, CancellationToken cancel, String arg, String[] options) at Ipfs.Http.FileSystemApi.ReadAllTextAsync(String path, CancellationToken cancel) at $.<

$>d__0.MoveNext() in /home/fady/ipfstest/Program.cs:line 6 --- End of stack trace from previous location --- at $.
(String[] args)

I'm using your sample code as per instructions on the readme page

using Ipfs.Http;

var ipfs = new IpfsClient();

const string filename = "QmXarR6rgkQ2fDSHjSY5nM2kuCXKYGViky5nohtwgF65Ec/about"; string text = await ipfs.FileSystem.ReadAllTextAsync(filename);

jurilents commented 2 years ago

Problem is caused by a breaking change in the HTTP interface of the IPFS daemon. Since version 0.5.0, all the endpoints accept only POST requests.

And this is my temp solution for this issue - override default methods using PostDownloadAsync instead of DownloadAsync (certainly not the best choice, but still works for me) 😅

public class CustomIpfsFileReader
{
    private readonly IpfsClient ipfs;

    internal CustomIpfsFileReader(IpfsClient ipfs)
    {
        this.ipfs = ipfs;
    }

    public async Task<string> ReadAllTextAsync(string path, CancellationToken cancel = default)
    {
        await using Stream data = await this.ReadFileAsync(path, cancel);
            using StreamReader reader = new StreamReader(data);
            return await reader.ReadToEndAsync();
    }

    public Task<Stream> ReadFileAsync(string path, CancellationToken cancel = default)
    {
        return this.ipfs.PostDownloadAsync("cat", cancel, path);
    }

    public Task<Stream> ReadFileAsync(string path, long offset, long length = 0, CancellationToken cancel = default)
    {
        return this.ipfs.PostDownloadAsync("cat", cancel, path, $"offset={offset}", $"length={length}");
    }

    public Task<IFileSystemNode> ListFileAsync(string path, CancellationToken cancel = default)
    {
        return this.ipfs.FileSystem.ListFileAsync(path, cancel);
    }

    public Task<Stream> GetAsync(string path, bool compress = false, CancellationToken cancel = default)
    {
        return this.ipfs.FileSystem.GetAsync(path, compress, cancel);
    }
}
Andrem19 commented 2 years ago

That helped. Thanks

Arlodotexe commented 1 year ago

@jurilents @Andrem19 @fadyanwar This library is no longer being maintained. The community has taken ownership of a fork in the IPFS Shipyard, and are keeping it maintained and updated.

See #72 for details.