Search code examples
c#asp.net-corehttprequestcontent-typecontent-disposition

Using NFTStorage WebApi (upload) with some problem and get an error "Response status code does not indicate success: 500 (Internal Server Error)."


And i use Postman to test is Body using "form-data" and "key" is file "value" is upload ur file, Headers u should set "Key" is Authorization "value" is Bearer yourapikey from NFT storage account. And this can work!

and here is mycode it will got an error "Response status code does not indicate success: 500 (Internal Server Error)." Can find the document in https://nft.storage/api-docs/

string path = "file.json"
const string uri = "https://api.nft.storage";
MultipartFormDataContent form = new MultipartFormDataContent();

var imagename = path.Split(@"\").ToList().Last();
var fileStreamContent = new StreamContent(System.IO.File.OpenRead(path));
ContentDispositionHeaderValue hd = new ContentDispositionHeaderValue(@"form-data");
hd.Name = "file";
hd.FileName = "metadata.json";
fileStreamContent.Headers.ContentDisposition = hd;
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
form.Add(fileStreamContent, "file", imagename);
form.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", "Bearer yourapikey");
    HttpResponseMessage response = await client.PostAsync(uri+"/upload", form);
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}

Solution

  • I find the problem is that

    hd.Name = @"""file""";

    hd.FileName = @"""metadata.json""";

    The Api have to add this " char in string