-1

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;
}
iop04329
  • 13
  • 6
  • A multipart form is a mime attachment that starts with a new line with two dashes. See example : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140)?force_isolation=true. I can't tell exactly what is failing. Either the MIME is not formatted correctly or the default HTTP headers in c# are not matching the headers in Postman. Is postman working from same computer and same account? Inside VS you are not automatically an ADMIN. Try starting VS by right clicking VS shortcut and select Run As Admin. – jdweng Aug 09 '22 at 09:22
  • I find the problem is that `hd.Name = @"""file""";` `hd.FileName = @"""metadata.json""";` The Api have to add this " char in string .... – iop04329 Oct 25 '22 at 08:07
  • The double quote in HTML has to be escaped. See : https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references?force_isolation=true – jdweng Oct 25 '22 at 08:41

1 Answers1

0

I find the problem is that

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

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

The Api have to add this " char in string

iop04329
  • 13
  • 6