cURL – POST request examples

cURL logo

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
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
newest oldest most voted
Preety
Guest
Preety

alert(“awesome articles”)

Borgy Manotoy
Guest
Borgy Manotoy

Nice! Is it possible to have a json POST with multiple files inside? Example: { “ticketId”: “1”, “ticketCode”: “ABC123”, “attachments”: [ FILES_HERE ] }? Thanks

ha
Guest
ha

hi kyong,

I am not a java developer but just testing a bit with REST API commands such us POST GET PUT and I wonder what should be the directives I need to modify/config in the http.conf file so I can enable/allow writing files to the webserver using the PUT command.. if you can please give me a example would be great… thanks in advance….

Lilly
Guest
Lilly

Thank you for this tutorial, please is it possible to combine a POST + JSON + array params? it will be like Multipart?

Sean
Guest
Sean

I’m brand new to everything web (java, Spring Boot, etc.) and I’m struggling a little with this example – namely, I’m getting a 404 error when I attempt to upload the file using cURL. Here’s the command line dump:

>curl -F file=@”./thumbsUpEmoji.png” http://localhost:8080/api/upload
{“timestamp”:”2020-04-16T13:48:37.951+0000″,”status”:404,”error”:”Not Found”,”message”:”No message available”,”path”:”/api/upload”}

And I copied the example code above (the one for the “/api/upload”) into my Rest Controller class. I have other mappings/functions in that same controller (mostly request mappings) and those all work when I fire up the server, so I think most things are working correctly, I’m just not able to upload a file.

Any thoughts?