【问题标题】:Why did the server respond with a status of 415 - API Post Method为什么服务器响应状态为 415 - API Post Method
【发布时间】:2020-07-26 18:56:33
【问题描述】:

我正在尝试使用 Spring Boot 创建自己的 API,该 API 目前使用从空气质量 API 访问外部数据。

我有一个 CityInfo 实体:

@Entity
public class CityInfo{
    @Id
    private String id;
    private String name;


    public CityInfo(){

    }

    public CityInfo(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
.
.
.
}

休息控制器:

    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private CityInfoRepository cityInfoRepository;

    @GetMapping("/CityInfo")
    public List<CityInfo> getAllCityInfo() {
        return cityInfoRepository.findAll();
    }

    @PostMapping ("/CityInfo")
    public void addCityInfo(@RequestBody CityInfo cityInfo) {
        cityInfoService.add(cityInfo);
    }

在发布到“localhost:port/CityInfo”时,邮递员可以正常工作 {"id":"1","name":"London"} 并在 "/CityInfo" 中读取。

当我尝试使用 JS 发布时,它返回错误 415,据说是“415 不支持的媒体类型”。

function postData(){
    let id = "31";
    let name = "CITYCITY"
    fetch('http://localhost:8084/CityInfo', {
        method: 'POST',
        body:JSON.stringify({"id":id,
                            "name":name})
    }).then((res) => res.text())
        .then((text)=>console.log("text:"+ text))
        .catch((err)=>console.log("err:" + err))
}
postData();

在控制台上返回: "加载资源失败:服务器响应状态为 415 ()"

我想我发送的 JSON 格式错误,但至少在我看来不是。

任何帮助都会很棒。Ty

编辑: 邮递员照片

function postData(){
    let id = "31";
    let name = "CITYCITY"
    fetch('http://localhost:8084/CityInfo', {
        method: 'POST',
        body:JSON.stringify({"id":id,
                            "name":name}),
        contentType: 'application/json',
        contentEncoding: 'gzip',
        contentEncoding: 'deflate',
        contentEncoding: 'br',
    }).then((res) => res.text())
        .then((text)=>console.log("text:"+ text))
        .catch((err)=>console.log("err:" + err))
}
postData()

它返回: 发布http://localhost:8084/CityInfo415

【问题讨论】:

    标签: javascript api spring-restcontroller


    【解决方案1】:

    documentation here 解释了 415 响应的含义。

    您的 postData 函数中的 Content-Type 或 Content-Encoding 可能有误。

    无论如何,您需要检查端点的预期并确保您的请求符合这些预期。

    【讨论】:

    • 感谢您的回答。关于出现的错误,我之前已经看过文档。据我了解,我的控制器获取它发布的信息并创建一个将添加到存储库中的 CityInfo 对象。我根本无法弄清楚为什么它可以与 Postman 一起正常工作,使用:{“id”:“1”,“name”:“London”},如果尝试使用 JS 函数,它将无法正常工作......它似乎我正在尝试发送格式错误的 JSON 文件,同时我认为它正在发送:{"id":"31","name":"CITYCITY"} 应该被接受
    • 您检查过您的 Content-Type 和 Content-Encoding 吗?
    • 所以我正在查看 Postman 应用程序。我发送了 Content-Type="application/json" 的 JSON 文件。知道了这一点,我尝试在创建 JSON 文件的 JS 中包含 content-type 属性(因为我不确定,我将添加一个关于我的邮递员应用程序的图像和适用的 JS 函数的代码)
    • 无效返回:POST localhost:8084/CityInfo415
    【解决方案2】:

    所以基本上我发送的是格式错误的 JSON 文档。 使用 Postman 时,它有 'Content-Type': 'application/json'

    这里是编辑后的 ​​JS:

    function postData(){
        let id = "31";
        let name = "CITYCITY"
        fetch('http://localhost:8084/CityInfo', {
            method: 'POST',
            body:JSON.stringify({"id":id,
                                "name":name}),
            headers: {
                'Content-Type': 'application/json'
            }
        }).then((res) => res.text())
            .then((text)=>console.log("text:"+ text))
            .catch((err)=>console.log("err:" + err))
    }
    postData()
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 2018-02-19
      • 2015-04-28
      • 1970-01-01
      • 2013-12-08
      • 2012-01-31
      相关资源
      最近更新 更多