I am using an external API to host my images on there since this is just a test. The API is a Minimal .NET Core API. I created a class with the properties that the objects will have:
Item Class
using MongoDB.Bson.Serialization.Attributes;
namespace Gyft.Data.Item
{
public class Item
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public IFormFile Image { get; set; }
}
}
In ItemsService Class, I have implemented a method to add an item into the database.
ItemsService Class
public async Task AddItem(Item item)
{
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri("https://api.imgbb.com/1/upload?key=*[KEY_REDACTED]*&image=" + item.Image),
Method = HttpMethod.Post,
};
var response = await client.SendAsync(request);
var str = await response.Content.ReadAsStringAsync();
await _items.InsertOneAsync(item);
}
Now, In Program.cs
file, following POST request is mapped:
app.MapPost("/api/items", async (ItemServices itemServices, Item newItem) =>
{
await itemServices.AddItem(newItem);
});
Postman returns a 415 error code. Am I going about it the right way?