cURL Post JSON data to Spring REST

摘要: This article shows you how to use cURL command to POST JSON data to a Spring REST API.

This article shows you how to use cURL command to POST JSON data to a Spring REST API.

1. Spring REST

A Simple Spring REST API to validate a login.

LoginController.java
package com.mkyong.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class LoginController {
    private final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @PostMapping("/api/login")
    public ResponseEntity<?> login(@RequestBody Login login) {
        logger.debug("login : {}", login);
        //validate login here
        return new ResponseEntity("Successfully login", new HttpHeaders(), HttpStatus.OK);
Login.java
package com.mkyong.controller;
public class Login {
    String username;
    String password;
    //getters and setters

2. cURL Post JSON

To test above REST API, you can use the cURL command to post a JSON data like this :

2.1 On Windows, you need to escape the double quotes

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

2.2 For *nix or Mac OSX, add a single quote like this :

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

2.3 --help

Terminal
$ curl --help
...
-d HTTP POST data
-H Pass custom header LINE to server
-X Specify request command to use

2.4 Display detail with curl -v

Terminal
c:\> curl -v -H "Content-Type: application/json" -X POST -d {\"username\":\"mkyong\",\"password\":\"abc\"} http://localhost:8080/api/login/
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /api/login/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 38
>
* upload completely sent off: 38 out of 38 bytes
< HTTP/1.1 200
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 18
< Date: Thu, 26 Jan 2017 08:00:03 GMT
<
Successfully login* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact

References

  1. Wikipedia - cURL
  2. Building REST services with Spring

上一篇: cURL POST request examples
下一篇: Spring Boot file upload example Ajax and REST
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

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

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

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