Free Support Forum - aspose.cloud

Bad request error when uploading to /v1.1/storage/file


#1

Hi. I’m trying to make a REST call to the storage but I get 'Bad Request – An error occurred while parse the multipart content: The file name could not have been retrieved." on the with the code example below. Any idea what I’m doing wrong?

  var client = new RestClient("https://api.aspose.cloud");

  // OAuth request... 
  RestRequest requestOAuth = new RestRequest("/oauth2/token", Method.POST);
  requestOAuth.AddHeader("Authorization", "Basic " + client);
  requestOAuth.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  requestOAuth.AddParameter("grant_type", "client_credentials");
  requestOAuth.AddParameter("client_id", "XXX");
  requestOAuth.AddParameter("client_secret", "XXX");
  IRestResponse responseOAuth = client.Execute(requestOAuth);

  // Convert the result to JSON
  JObject result = JObject.Parse(responseOAuth.Content);

  var requestStorePDF = new RestRequest("/v1.1/storage/file", Method.PUT);
  requestStorePDF.AddHeader("Authorization", "Bearer " + result.GetValue("access_token").ToString());
  requestStorePDF.AddHeader("Content-Type", "multipart/form-data");
  requestStorePDF.AddParameter("path", "test.pdf");
  requestStorePDF.AddParameter("file", "test.pdf");
  requestStorePDF.AddFile("test.pdf", "E:\\Documents\\test.pdf");
  IRestResponse responseStore = client.Execute(requestStorePDF);

#2

@willm1716

Thanks for your inquiry. It seems some RestSharp related issue. The Upload file is working fine with cURL and Aspose.Storage Cloud SDK for .NET. Please install Aspsoe.Storage Cloud SDK for .NET from NuGet and try following code snippet. However, if you are interested only the use of RestSharp then please let us know, we will investigate it further.

// For complete examples and data files, please go to https://github.com/aspose-storage-cloud/aspose-storage-cloud-dotnet
// Get App Key and App SID from https://dashboard.aspose.cloud/
var configuration = new Configuration { AppSid = MyAppSid, AppKey = MyAppKey };
StorageApi storageApi = new StorageApi(configuration);

var fileName = "test.pdf";
PutCreateRequest request = new PutCreateRequest(fileName, File.OpenRead(@"c:\Temp\" + fileName), null, null);
var response = storageApi.PutCreate(request);