I need to send both user input as JSON data and a file to an API end point. I can send each independently , but I cannot for the life of me figure out how to do it with both.
My API end point reads each just fine, but upon debug doing them together, the request never makes it past the Axios post call.
I've tried a number of tests to get this going, but no success so far.
*EDIT Turns out half the issue was on the server side. Using .Net 5 API and I was using IFormFile file and UserMetaData data as parameters. I found an article creating all POCOs in a model and using that. instead. I can now properly receive the file and data using POSTMAN, but it's now null when coming from axios. Here's the controller for reference
[HttpPost]
[Route("user-upload")]
public async Task<IActionResult> UserUpload([FromForm] UserMetaData userData)
{
var result = " You sent the value " + userData;
return Ok(userData.UserFile.FileName);
}
Frustratingly now when I send the same data via axios, it hits the controller and it's null. I've verified that the data is there before arriving. Can anyone see something I missed ?
Here is some code to show :
Test Object
let inputTest = {
dataOwner: this.name,
deviceSerialNumber: this.deviceSerialNumber,
dataDistribution: this.distribution,
}
// New FormData to append file and user data
let formData = new FormData();
//File input
for (let files of this.file) {
formData.append("userFile", files);
}
//User data
formData.append("userData", JSON.stringify(inputTest));
//Axios call
this.axios.post('API endpoint URL',
formData,{
header: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
},
}
).then(response =>{ console.log(response)}).catch(error =>{ alert(error)})
I've tried changing the headers and content type, both together and separate. No luck.
I get this error when posting:
**status: 415**
**title: "Unsupported Media Type"**
I don't want to send these independent of one another as they completely voids the app needs.
Grateful for your help