How do I POST JSON data with cURL?

后端 未结 24 2868
刺人心
刺人心 2020-11-21 23:56

I use Ubuntu and installed cURL on it. I want to test my Spring REST application with cURL. I wrote my POST code at the Java side. However, I want to test it with cURL. I am

24条回答
  •  深忆病人
    2020-11-22 00:36

    It worked for me using:

    curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/api/postJsonReader.do
    

    It was happily mapped to the Spring controller:

    @RequestMapping(value = "/postJsonReader", method = RequestMethod.POST)
    public @ResponseBody String processPostJsonData(@RequestBody IdOnly idOnly) throws Exception {
            logger.debug("JsonReaderController hit! Reading JSON data!"+idOnly.getId());
            return "JSON Received";
    }
    

    IdOnly is a simple POJO with an id property.

提交回复
热议问题