简体   繁体   English

邮递员在尝试使用表单数据执行 POST 时返回 415,但与 JSON 相同的请求工作正常

[英]Postman returns 415 when trying to do a POST using form-data, but the same request with JSON works fine

I'm building a REST API and testing it out using Postman.我正在构建一个 REST API 并使用 Postman 对其进行测试。 I have an end-point which works fine when I test it by sending in raw json data, but I want to expand on this endpoint and allow it to take both json data and accept a file, so I wanted to test my current endpoint without any modifications, and see if I would get back the same result when I test my API using form-data instead of JSON, but it always throws a 415 exception.我有一个端点,当我通过发送原始 json 数据来测试它时工作正常,但我想扩展这个端点并允许它同时获取 json 数据并接受一个文件,所以我想测试我当前的端点而不任何修改,看看当我使用表单数据而不是 JSON 测试我的 API 时是否会得到相同的结果,但它总是抛出 415 异常。

On this picture I make a request with form-data.在这张图片上,我使用表单数据发出请求。

表单数据请求

And here I make the request to the same endpoint but with json data在这里我向同一个端点发出请求,但使用 json 数据

JSON 发布数据请求

Note that I have not added any customs headers when sending the requests, the (10) you see in the top is Temporary Headers.请注意,我在发送请求时没有添加任何海关标头,您在顶部看到的 (10) 是临时标头。 I also tried adding Content-Type: multipart/form-data, but got the same result.我也尝试添加 Content-Type: multipart/form-data,但得到了相同的结果。

Here's the code behind这是后面的代码

PeopleController.cs人员控制器.cs

[HttpPost]
    public ActionResult<PersonDto> PostPerson(PersonForCreationDto person)
    {
        var personEntity = _mapper.Map<Entities.Person>(person); //Maps PersonForCreationDto to Entites.Person. This is possible because of the mapping in PeopleProfile.cs
        _personLibraryRepositry.AddPerson(personEntity);
        _personLibraryRepositry.Save();

        var personToReturn = _mapper.Map<PersonDto>(personEntity);

        return CreatedAtRoute("GetPerson",
            new { personId = personToReturn.PersonId },
            personToReturn);
    }

PersonForCreationDto创造者

public class PersonForCreationDto
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string ReasonsForBeingOnTheList { get; set; }

        public ICollection<PictureForCreationDto> Pictures { get; set; }
            = new List<PictureForCreationDto>();


    }

PersonLibraryRepository.cs PersonLibraryRepository.cs

 public void AddPerson(Person person)
    {
       if (person == null)
        {
            throw new ArgumentNullException(nameof(person));
        }

        person.PersonId = Guid.NewGuid(); //API is responsibile for creating new IDS.

            foreach (var picture in person.Pictures)
            {
                picture.PictureId = Guid.NewGuid();

            }
        _context.People.Add(person);
    }

在您的表单数据中,将此添加到标题

Content-Type: application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持的媒体类型 415,但在 Postman 中工作正常 - Unsupported Media Type 415, but in Postman works fine Webrequest返回401,但使用邮递员作品的请求相同 - Webrequest returns 401 but same request using postman works 当Post-Type未在Postman中设置为application / json时,WCF返回HTTP 400,否则它可以正常工作。 - WCF returns HTTP 400 when Content-Type is not Set to application/json in Postman, else it works fine. 我的 POST 请求在 c# 中失败,但在 POSTMAN 中运行良好 - My POST request fails in c# but works perfectly fine in POSTMAN HttpClient post 在 C# 中返回错误的请求,在邮递员中工作 - HttpClient post returns bad request in c#, works in postman MVC Post 请求总是返回 IIS 错误 500 但与邮递员一起工作 - MVC Post request always returns IIS error 500 but works with postman 为什么相同的POST请求在POSTMAN中起作用,但在浏览器AJAX中却不起作用(找不到404)? - Why the same POST request works in POSTMAN but not in browser AJAX (404 Not Found)? 如何在 C# 中使用表单数据构造 HttpClient POST 请求? - How to construct HttpClient POST Request with form-data in C#? 如何使用正文选项表单数据进行发布请求 - How to make a post request with body option form-data 如何在 Xamarin 中的同一 API 中传递表单数据和 json? - How to pass form-data and json in same API in Xamarin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM