I'm trying after "converted/optimized" a C# Unity Project to my Visual Studio in C# to work for me (the code)...
i used this github lib for nft.storage https://github.com/filipepolizel/unity-nft-storage/blob/main/NFTStorageClient.cs
If i call then with:
string pathtofile = @"" AppDomain.CurrentDomain.BaseDirectory "testfile.txt";
await NFTStorageTest.Upload(NFTStorageTest.nftStorageApiUrl, pathtofile);
i get this error:
System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at Main.net.NFTStorageTest.Upload(String uri, String pathFile) in C:\...\NFTStorageTest.cs:line 173
Here is my Code:
// nft.storage API endpoint
internal static readonly string nftStorageApiUrl = "https://api.nft.storage/";
// HTTP client to communicate with nft.storage
internal static readonly HttpClient nftClient = new HttpClient();
// http client to communicate with IPFS API
internal static readonly HttpClient ipfsClient = new HttpClient();
// nft.storage API key
public string apiToken = "XXX";
public void Start()
{
nftClient.DefaultRequestHeaders.Add("Accept", "application/json");
if (apiToken != null)
{
nftClient.DefaultRequestHeaders.Add("Authorization", "Bearer " apiToken);
}
else
{
// log in console in case no API key is found during initialization
Console.WriteLine("Starting NFT Storage Client without API key, please call 'SetApiToken' method before using class methods.");
}
}
public async Task<NFTStorageUploadResponse> UploadDataFromFile(string path)
{
StreamReader reader = new StreamReader(path);
string data = reader.ReadToEnd();
reader.Close();
Console.WriteLine("Uploading...");
return await UploadDataFromString(data);
}
public async Task<NFTStorageUploadResponse> UploadDataFromString(string data)
{
string requestUri = nftStorageApiUrl "/upload";
string rawResponse = await Upload(requestUri, data);
string jsonString = JsonConvert.SerializeObject(rawResponse);
NFTStorageUploadResponse parsedResponse = JsonConvert.DeserializeObject<NFTStorageUploadResponse>(jsonString);
return parsedResponse;
}
public static async Task<string> Upload(string uri, string pathFile)
{
byte[] bytes = File.ReadAllBytes(pathFile);
using (var content = new ByteArrayContent(bytes))
{
content.Headers.ContentType = new MediaTypeHeaderValue("*/*");
//Send it
var response = await nftClient.PostAsync(uri, content);
response.EnsureSuccessStatusCode();
Stream responseStream = await response.Content.ReadAsStreamAsync();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
}`
whats my mistake and how can i fix it?
CodePudding user response:
await NFTStorageTest.Upload(NFTStorageTest.nftStorageApiUrl, pathtofile);
This should be called with
$”{NFTStorageTest.nftStorageApiUrl}upload”
See the original file https://github.com/filipepolizel/unity-nft-storage/blob/45bedb5c421982645bc9bf49d12beed8f2cdb9d3/NFTStorageClient.cs#L297