0

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?

Rena
  • 30,832
  • 6
  • 37
  • 72
Reznov
  • 63
  • 8
  • I intend to somehow get the data from response in JSON so I can get the link of the uploaded image. I will be able to then store that link along with the item's other information in a document. – Reznov Aug 28 '22 at 21:48
  • your external API expects image in url or inside body ? be sure about it. based on that you might need to read the image before passing on to your external api call. – CodingMytra Aug 29 '22 at 04:46
  • @CodingMytra I have been sending image data from the Postman in the "form-data" in "body" of the request. How am I to send it in the form-data? – Reznov Aug 29 '22 at 04:56
  • are you talking about external api or your api ? – CodingMytra Aug 29 '22 at 04:59
  • I am saying that when I send api request to external api to upload the image, i should send it as form data and not as a parameter. I am asking how am I to do that since I am new to this. – Reznov Aug 29 '22 at 05:05
  • here is one [example](https://stackoverflow.com/a/72798515/9247039) . have a look if this gives you some idea – CodingMytra Aug 29 '22 at 05:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247636/discussion-between-reznov-and-codingmytra). – Reznov Aug 29 '22 at 05:25

1 Answers1

1

Binding from form values is not supported in .NET 6. So here is a workaround that you can refer:

builder.Services.AddScoped<ItemServices>();

//other middleware or services....

app.MapPost("/api/items", async ([FromServices]ItemServices itemServices,HttpRequest request) =>
{
    var data = request.Form;
    var newItem = new Item()
    {
        Id = data["Id"],
        Description = data["Description"],
        Title = data["Title"],
        Image = data.Files[0]
    };
    await itemServices.AddItem(newItem);
});
Rena
  • 30,832
  • 6
  • 37
  • 72
  • It was something good since I can add the data and is getting read properly while debugging. But I am getting another exception that I have not seen before. I am not sure why it is getting thrown but I'm positive that it is getting thrown by the part where I am actually adding the data to the database. – Reznov Aug 30 '22 at 21:30
  • Hi @Reznov, I think it should be another question, you need post a new thread and share the database operation code with thrown exception. – Rena Aug 31 '22 at 01:10
  • 1
    Great. I can post the question and I will share the link with you too if you wanna get in on the action lol – Reznov Aug 31 '22 at 07:51