im trying to http post to a rest api where i need the post content to be like this
{
"_id": "string",
"minDuration": 0,
"maxDuration": 0,
"calculatedMaxLimitDuration": 0,
}
but i dont know much about java and arrays(i guess) as im use to php and havent coded in years so what should i put in the.POST()???
public class App {
static String lockId = "secret";
private static final String POSTS_API_URL = "Url" + lockId + "/update-time";
public static void main( String[] args ) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.POST()
.header("accept", "application/json")
.header("Authorization", "Bearer secret")
.uri(URI.create(POSTS_API_URL))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
I suggest you use RestTemplate or WebClient to call the API, it would be easy !
A snippet as below
{
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity <String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange("
http://serviceToCall:8080/endPoint", HttpMethod.POST, entity, String.class).getBody();
}
Read more about restTemplate here https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.