Home > Back-end >  Postman send both data in json and image
Postman send both data in json and image

Time:11-24

I have created one API and it is adding data in database, with values and image.

In postman I am calling that and setting image in form-data and other values in raw in JSON but still in image I am getting null value in source code object. Below are screenshots.

I want help in two things :

  1. Why I am getting Null in image value in source code, please check below image.
  2. How can I send both image and data using postman, check below screenshots is that ok what I have tried ?

First screenshot is of postman in which I am sending data in json format :

enter image description here

Second screenshot is of postman in which I am sending image in same request in postman

enter image description here

Now in third image you can see in Image it is showing Null instead of values.

enter image description here

So I am not getting why image is coming null..Below is code for this

public async Task<int> Create(UserPreference _object)
{
   if(_object.Image != null)
   {
       var webRootPath = hostingEnv.WebRootPath;
       var fileName = Path.GetFileName(_object.Image.FileName);
       var filePath = Path.Combine(hostingEnv.WebRootPath, "images\\User", fileName);

       using(var fileStream = new FileStream(filePath, FileMode.Create))
       {
          await _object.Image.CopyToAsync(fileStream);
       }

       _object.ImagePath = filePath;
   }
   
   var obj = await applicationDbContext.UserPreferences.AddAsync(_object);
   return applicationDbContext.SaveChanges();
}

CodePudding user response:

enter image description hereYou cannot send both form-data and JSON body at the same time To solve your problem send image and your other information as form-date For more details see the screenshot

CodePudding user response:

To send image and data using Postman you have to use form-data content type.

Select body => form-data. Column key has a selector. For a file select file-type and select file. To send data in the same time you will have to separate your json in key value pair.

  • Related