To post JSON data with Curl, use the -X POST option and pass the JSON data using the -d command line parameter, with the Content-Type set to -H “Content-Type: application/json”. The curl post with json data uses the curl command to send JSON data to the HTTP POST method. Curl post json data to the web servers that runs micro web services. In order to post json data, the curl command should include a header -H “Content-Type: application/json”. This informs to the server about the content type as JSON data.
The Restful service is generated by using the Rest Controller in the spring boot application. The Rest Controller can listen to any restful site request that may contain JSON data. The JSON data is generated and sent using the HTTP POST method. You can post JSON data using CURL, Postman, or by programming languages such as Java.
Curl is a command line tool that helps users to make network requests. Curl is widely used by developers to create a request for a JSON post in the spring boot application. Curl is available on Linux, Mac, and Windows.
How to post a JSON data with curl – In this article, we can see about How to POST a JSON data with CURL, How to POST a JSON file with CURL, How to POST a JSON request using CURL to spring boot rest controller methods, How to POST a JSON data using CURL with proxy server.
Simple Curl POST Request
The first basic POST curl call is invoked using the curl command. The command line argument includes the HTTP POST method that uses the -X option. The last argument on the command line is the web service url.
The curl command in the command line will be seen as below.
Command
curl -X POST http://localhost:8080/welcome
Spring boot Rest Controller class
package com.yawintutor.application;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome", method=RequestMethod.POST)
public String welcomepage() {
return "Welcome to Yawin Tutor\n";
}
}
Output
$ curl -X POST http://localhost:8080/welcome
Welcome to Yawin Tutor
CURL POST Request for @RequestParam
The spring boot application data sent via the http request body will be received using the @RequestParam annotation. The example below shows how to make a curl call to send data in the request body and how to retrieve it using the @RequestParam annotation.
curl -X POST -d 'username=yawin&password=tutor' http://localhost:8080/welcome
package com.yawintutor.application;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome", method=RequestMethod.POST)
public String welcomepage(@RequestParam("username") String username, @RequestParam("password") String password) {
return "Welcome to Yawin Tutor\nUsername:"+username+" Password:"+password+"\n";
}
}
Output
$ curl -X POST -d 'username=yawin&password=tutor' http://localhost:8080/welcome
Welcome to Yawin Tutor
Username:yawin Password:tutor
CURL POST Request for @PathVariable
The spring boot application supports passing values as part of the request url. The path value can be obtained using the @PathVariable annotation.
curl -X POST http://localhost:8080/welcome/yawin/tutor
package com.yawintutor.application;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome/{username}/{password}", method=RequestMethod.POST)
public String welcomepage(@PathVariable("username") String username, @PathVariable("password") String password) {
return "Welcome to Yawin Tutor\nUsername:"+username+" Password:"+password+"\n";
}
}
Output
$ curl -X POST http://localhost:8080/welcome/yawin/tutor
Welcome to Yawin Tutor
Username:yawin Password:tutor
CURL POST Request using JSON data
The spring boot application sends the data to JSON as part of the request body. The spring boot rest controller will parse the JSON and translate it to a value object.
curl -X POST -H 'Content-Type:application/json' -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome
package com.yawintutor.application;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome", method=RequestMethod.POST)
public String welcomepage(@RequestBody Login login) {
System.out.println(login.getUsername()+" "+login.getPassword());
return "Welcome to Yawin Tutor\nUsername:"+login.getUsername()+" Password:"+login.getPassword()+"\n";
}
}
package com.yawintutor.application;
public class Login {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Output
$ curl -X POST -H 'Content-Type:application/json' -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome
Welcome to Yawin Tutor
Username:yawin Password:tutor
CURL POST Request using JSON data with Headers
You can send multiple headers along with the request body to the curl command. The JSON data is sent in the request body and the headers are sent using the -H option.
curl -X POST -H 'Content-Type:application/json' -H "Accept:application/json" -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome
package com.yawintutor.application;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome", method=RequestMethod.POST)
public String welcomepage(@RequestBody Login login, @RequestHeader Map<String, String> headerMap) {
String str = "Welcome to Yawin Tutor\nUsername:"+login.getUsername()+" Password:"+login.getPassword()+"\n";
for(Map.Entry<String, String> entry: headerMap.entrySet()) {
str = str+"key:"+entry.getKey()+" value:"+entry.getValue()+"\n";
}
return str;
}
}
Output
$ curl -X POST -H 'Content-Type:application/json' -H "Accept:application/json" -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome
Welcome to Yawin Tutor
Username:yawin Password:tutor
key:host value:localhost:8080
key:user-agent value:curl/7.54.0
key:content-type value:application/json
key:accept value:application/json
key:content-length value:39
CURL POST Request using JSON file
The curl command allows to send JSON data using the json file. The json data is added to the file and the file name is added with the @ prefix.
$ curl -X POST -H 'Content-Type:application/json' -H "Accept:application/json" -d '@data.json' http://localhost:8080/welcome
data.json
{"username":"yawin","password":"tutor"}
package com.yawintutor.application;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/welcome", method=RequestMethod.POST)
public String welcomepage(@RequestBody Login login, @RequestHeader Map<String, String> headerMap) {
String str = "Welcome to Yawin Tutor\nUsername:"+login.getUsername()+" Password:"+login.getPassword()+"\n";
for(Map.Entry<String, String> entry: headerMap.entrySet()) {
str = str+"key:"+entry.getKey()+" value:"+entry.getValue()+"\n";
}
return str;
}
}
Output
$ curl -X POST -H 'Content-Type:application/json' -H "Accept:application/json" -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome
Welcome to Yawin Tutor
Username:yawin Password:tutor
key:host value:localhost:8080
key:user-agent value:curl/7.54.0
key:content-type value:application/json
key:accept value:application/json
key:content-length value:39
CURL POST Request using JSON data with HTTP/HTTPS Proxy Server
The proxy server setting should be added using the -x option in the curl command. The curl command will be seen as like below.
curl -X POST -H 'Content-Type:application/json' -H "Accept:application/json" -x http://proxyhostname:proxyport --proxy-user proxyusername:proxypassword -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome
CURL POST Request using JSON data with SOCK Proxy Server
The proxy server setting should be added using the -x option in the curl command. The curl command will be seen as like below.
curl -X POST -H 'Content-Type:application/json' -H "Accept:application/json" --socks5-hostname proxyhostname:proxyport --proxy-user proxyusername:proxypassword -d '{"username":"yawin","password":"tutor"}' http://localhost:8080/welcome