Help - C# - API - Upload Component - No assets found in upload - Sonatype Nexus Repository - Sonatype Community
Help - C# - API - Upload Component - No assets found in upload
post by josh.a.corcoran on Mar 8, 2024
I am new to Nexus and I am trying to upload a file to a Nexus Repo. I have pulled the swagger endpoint and looked through the help documentation and everything seems to be correct, in fact it worked once prior to just stopping. I am using the following C# code to post my request to our local Nexus API instance and receiving the following error message in the response:
Status Code: BadRequest (400)
[\
{\
"id": "*",\
"message": "No assets found in upload"\
},\
{\
"id": "asset1",\
"message": "Unknown component field 'asset1'"\
},\
{\
"id": "asset1.filename",\
"message": "Unknown component field 'asset1.filename'"\
}\
]
Here is my C# Code:
private bool InsertComponent()
{
//Local Variable Declaration
var returnValue = false;
var form = default(System.Net.Http.MultipartFormDataContent);
var response = default(System.Net.Http.HttpResponseMessage);
var responseStr = string.Empty;
var responseObj = default(Newtonsoft.Json.Linq.JObject);
try
{
using (var Client = new System.Net.Http.HttpClient())
{
//Add the authorization token to the request header
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", "<Base64Creds>");
//Build Form Object
form = new System.Net.Http.MultipartFormDataContent();
form.Add(new System.Net.Http.StringContent("/Website/Supplier/JoshTest"), "raw.directory");
form.Add(new System.Net.Http.StringContent("Josh.txt"), "raw.asset1.filename");
//Add the image to form
var Image = System.IO.File.ReadAllBytes(@"J:\Projects\Nexus Research\Josh.txt");
form.Add(new System.Net.Http.ByteArrayContent(Image, 0, Image.Length), "raw.asset1");
//Post to the API
response = Client.PostAsync(this.NexusUrlBase + "/service/rest/v1/components?repository=repo-release-packages", form).Result;
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
//Get Server Response
responseStr = response.Content.ReadAsStringAsync().Result;
if (!string.IsNullOrEmpty(responseStr))
{
//Convert from raw json string into a JSON Object
responseObj = Newtonsoft.Json.Linq.JObject.Parse(responseStr);
if (responseObj != null)
{
}
}
else
{
throw new Exception("Failed to insert image into digital media storage bucket. Empty Response Returned");
}
}
else
{
//Get Server Response
responseStr = response.Content.ReadAsStringAsync().Result;
if (!string.IsNullOrEmpty(responseStr))
{
System.IO.File.WriteAllText(@"J:\Projects\Nexus Research\error.json", Newtonsoft.Json.Linq.JArray.Parse(responseStr).ToString(Newtonsoft.Json.Formatting.Indented));
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return returnValue;
}
It did work once the other day and uploaded the text file without issue but then suddenly a few days later I started receiving the bad request error.
I have looked at the following documentation and everything appears to be correct, please help
post by steviecoaster on Mar 8, 2024
Are you attempting to upload a file that already exists again? If so, it sounds like you have your Deployment Policy for the repo-release-packages repository set to Disable redeploy.
post by josh.a.corcoran on Mar 8, 2024
@steviecoaster No, this is a new file, in a new folder. I was able to initially perform the upload. I deleted the original file and folder and have not been able to upload a new one to that same location or a different one since. Does that help? I am able to use curl to do it through using the exact same form tags but not calling it through C#.
post by steviecoaster on Mar 9, 2024
I’m assuming the json from your initial response is what you’re receiving:
[\
{\
"id": "*",\
"message": "No assets found in upload"\
},\
{\
"id": "asset1",\
"message": "Unknown component field 'asset1'"\
},\
{\
"id": "asset1.filename",\
"message": "Unknown component field 'asset1.filename'"\
}\
]
Those id's aren’t right. They should be raw.asset1 and raw.asset1.filename, which you are properly setting in the form data from what I can see, so I’m not sure why those are coming back that way, unless we’re not seeing the entire code.
Is there a reason this is in C#? Part of a bigger project?
post by josh.a.corcoran on Apr 3, 2024
Yep, that is exactly what I am seeing and like it said it is so strange because the first time I ever ran it worked and then all subsequent times no matter what I do I always get that same error. We are a .NET shop and so that is the language standard that we use. That is the entire codebase that I am using minus my actual credentials and local Url path.