HTTP POST using JSON in Java

前端 未结 11 992
南笙
南笙 2020-11-22 07:24

I would like to make a simple HTTP POST using JSON in Java.

Let\'s say the URL is www.site.com

and it takes in the value {\"name\":\"mynam

11条回答
  •  情歌与酒
    2020-11-22 08:19

    I recomend http-request built on apache http api.

    HttpRequest httpRequest = HttpRequestBuilder.createPost(yourUri, String.class)
        .responseDeserializer(ResponseDeserializer.ignorableDeserializer()).build();
    
    public void send(){
       ResponseHandler responseHandler = httpRequest.execute("details", yourJsonData);
    
       int statusCode = responseHandler.getStatusCode();
       String responseContent = responseHandler.orElse(null); // returns Content from response. If content isn't present returns null. 
    }
    

    If you want send JSON as request body you can:

      ResponseHandler responseHandler = httpRequest.executeWithBody(yourJsonData);
    

    I higly recomend read documentation before use.

提交回复
热议问题