cURL POST request examples

摘要: Some cURL POST request examples for self reference.

Some cURL POST request examples for self reference.

1. Normal POST

1.1 To POST without data.

$ curl -X POST http://localhost:8080/api/login/

1.2 To POST with data.

$ curl -d "username=mkyong&password=abc" http://localhost:8080/api/login/

1.3 Spring REST to accept normal POST data.

    @PostMapping("/api/login")
    public ResponseEntity<?> login(@RequestParam("username") String username,
                                    @RequestParam("password") String password) {
        //...
    @PostMapping("/api/login")
    public ResponseEntity<?> login(@ModelAttribute Login login) {
        //...

2. POST + Multipart

To POST with a file, add this -F file=@"path/to/data.txt"

2.1 Upload a file

$ curl -F file=@"path/to/data.txt" http://localhost:8080/api/upload/

2.2 Upload multiple files, with extra fields :

$ curl -F extraField="abc" -F files=@"path/to/data.txt" -F files=@"path/to/data2.txt"  http://localhost:8080/api/upload/multi/

2.3 Spring REST to accept POST Multipart data.

    @PostMapping("/api/upload")
    public ResponseEntity<?> uploadFile(
            @RequestParam("file") MultipartFile uploadfile) {
        //...
    @PostMapping("/api/upload/multi")
    public ResponseEntity<?> uploadFiles(
            @RequestParam("extraField") String extraField,
            @RequestParam("files") MultipartFile[] uploadfiles) {
        //...
    @PostMapping("/api/upload/multi2")
    public ResponseEntity<?> uploadFiles2(
            @ModelAttribute UploadModel model) {
        //...

3. POST + JSON

To POST with JSON data, add this -H "Content-Type: application/json"

3.1 On Windows, escape the double quotes

c:\> curl -H "Content-Type: application/json" -X POST -d {\"username\":\"mkyong\",\"password\":\"abc\"} http://localhost:8080/api/login/

3.2 For *nix or Mac OSX, add a single quote

$ curl -H "Content-Type: application/json" -X POST -d '{"username":"mkyong","password":"abc"}' http://localhost:8080/api/login/

3.3 Spring REST to accept POST JSON data.

    @PostMapping("/api/login")
    public ResponseEntity<?> login(@RequestBody Login login) {
        //..

References

  1. cURL official website
  2. Wikipedia – cURL
  3. Building REST services with Spring
  4. cURL – Post JSON data to Spring REST
  5. Spring Boot file upload example – Ajax and REST

上一篇: jQuery Ajax submit a multipart form
下一篇: cURL Post JSON data to Spring REST
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号