2

I'm creating a simple Web API using .Net core 3.1 and I am having issues successfully posting a dictionary using Postman.

I have the following controller action:

[HttpPost("{id}/metas")]
[ProducesResponseType(typeof(Meta), 201)]
public async Task<IActionResult> CreateMeta([FromForm] NewMeta newMeta)
{
    return Ok();
}

The NewMeta currently looks like this:

public class NewMeta
{

    /// <summary>
    /// File descriptors
    /// </summary>
    public Dictionary<string, object> Refs { get; set; }

    /// <summary>
    /// Associated file
    /// </summary>
    public IFormFile File { get; set; }

}

I am trying to use Postman to POST dictionary data as well as a file.

I've tried using the various Key/Value settings within postman. An example shown in the images.

enter image description here

Results in :

enter image description here

How can i pass in a value? This API is early in its development so I can change anything that is needed.

Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
  • Your dictionary is of type `string, object` but your passsing in `string, int` – Train Dec 06 '19 at 20:34
  • I don't think you can use plain `object` for Web API Calls. Try something along these lines: https://stackoverflow.com/a/46868313/2720343 – Dortimer Dec 06 '19 at 20:35
  • FML...Thanks folks. Figured I could use Object to keep it available for stirng/int/bool. Doesn't seem that is an easy option though. Changing to string works. – Darren Wainwright Dec 06 '19 at 20:37
  • 1
    You could also send JSON from the body. It works with the objects too. – Hadi Samadzad Dec 06 '19 at 20:42

2 Answers2

1

Use public Dictionary<string, string> Refs in view model in controller and map to Dictionary<string, object>.

XouDo
  • 945
  • 10
  • 19
0

Your form-data as to be cast by the framework into a NewMeta object.

That mean the structure of your data has to be exactly the same than what your controller expected. So you need only 2 keys : Refs (as a json dictionary) and File.

Ashijo
  • 90
  • 8