I am using a micro service architecture, in API gateway, I get a request and I would like to send that request which contains List<PictureItemDto> pictures to Attachment Microservice and store picture information.
I have these model classes in both the Gateway and the Attachment MicroService:
public class PictureDto
{
public List<PictureItemDto> pictures { get; set; }
}
public class PictureItemDto
{
public string seoFilename { get; set; }
public string altAttribute { get; set; }
public string titleAttribute { get; set; }
public IFormFile file { get; set; }
}
And this is how I send data from API gateway to Attachment MicroService:
public async Task<bool> Create(List<PictureItemDto> postPictures)
{
MultipartFormDataContent formDataContent = new MultipartFormDataContent();
for (int i = 0; i < postPictures.Count; i++)
{
var item = postPictures[i];
formDataContent.Add(new StringContent(item.seoFilename), $"pictures[{i}].seoFilename");
formDataContent.Add(new StringContent(item.altAttribute), $"pictures[{i}].altAttribute");
formDataContent.Add(new StringContent(item.titleAttribute), $"pictures[{i}].titleAttribute");
StreamContent fileContent = new(item.file.OpenReadStream())
{
Headers =
{
ContentLength = item.file.Length,
ContentType = new MediaTypeHeaderValue(item.file.ContentType)
}
};
formDataContent.Add(fileContent, $"pictures[{i}].file", item.file.FileName);
}
var result = await _httpClient.PostAsync($"{_urlsOptions.AttachmentUrl}", formDataContent);
return (result.StatusCode == HttpStatusCode.OK) ? true : false;
}
My problem is that the data is received null in CreatePostPictures action(this action is in attachment microservice) :
[HttpPost("create")]
public async Task<IActionResult> CreatePostPictures([FromForm]PictureDto dto)
{
return Ok();
}
This is curl that I am sending :
curl --location --request POST 'http://localhost:80/api/Post?api-version=1.0' \
--header 'accept: */*' \
--header 'api-version: 1.0' \
--header 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJrYXZlaEBnbWFpbC5jb20iLCJqdGkiOiJlYjhkZmIyMy0wYTAxLTQzOWEtOWM1NS01Y2NmY2I2NmY3YzgiLCJ1bmlxdWVfbmFtZSI6ImthdmVoQGd' \
--form 'postPictures[0].titleAttribute="titleAttribute"' \
--form 'postPictures[0].seoFilename="seoFilename"' \
--form 'postPictures[0].altAttribute="altAttribute"' \
--form 'postPictures[0].File=@"/F:/Photos/WallPapers Pack 2011/Series 2/1024x768/WP2011.2 1024x768 (8).jpg"' \
--form 'postPictures[1].titleAttribute="titleAttribute1"' \
--form 'postPictures[1].seoFilename="seoFilename1"' \
--form 'postPictures[1].altAttribute="altAttribute1"'
--form 'postPictures[1].File=@"/F:/Photos/WallPapers Pack 2011/Series 2/1024x768/WP2011.2 1024x768 (6).jpg"' \