Skip to main content

Hi - I am trying to upload a file using ASP.NET WEB API, please let me know what am I doing wrong in the below code.

var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " XXX);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

using (var content = new MultipartFormDataContent())
{
var newFile = new { name = "test.txt", parent = new { id = 234} } ;

content.Add(new StreamContent(File.OpenRead(@"C:\\test.txt")), "Test", "Test.txt");

content.Add(new StringContent(JsonConvert.SerializeObject(newFile), Encoding.UTF8, "application/json"));
var req = new HttpRequestMessage(HttpMethod.Post, "https://upload.box.com/api/2.0/files/content") { Content = content };


var response = client.SendAsync(req).Result;

var result = await response.Content.ReadAsStringAsync();
}

 

I am getting below error -

"{\"type\":\"error\",\"status\":400,\"code\":\"bad_request\",\"context_info\":{\"errors\":[{\"reason\":\"missing_parameter\",\"name\":\"parent\",\"message\":\"'parent' is required\"}]},\"help_url\":\"http:\\/\\/developers.box.com\\/docs\\/#errors\",\"message\":\"Bad Request\",\"request_id\":\"***number removed for privacy***54f9e680df25455f5\"}"

 You might be seeing this error because you are setting the value of parent object id as an int instead of a string. Can you please retry the request by changing the parent object id type as shown below?

 

Change This Line:

var newFile = new { name = "test.txt", parent = new { id = 234} } ;

To:

var newFile = new { name = "test.txt", parent = new { id = "234"} } ;


Murtza - I changed the Id type to string, that did not solve the issue. 


I found the solution, the Upload endpoint expects parent_id not the parent object as mentioned in the documentation (that's how I was able to upload the file using .NET HttpClient).

 

content.Add(new StringContent({fileId}), "parent_id"); 

 

Thank you for your prompt support.

 

Sid