简体   繁体   English

如何使用Jetty http客户端将JSON作为请求正文发布?

[英]How to POST json as request body with Jetty http client?

I'm stuck with adding JSON to POST request body. 我坚持将JSON添加到POST请求正文中。 There just isn't any method that sets the body of the request. 只是没有任何方法可以设置请求的主体。

Is there any way I can achieve this. 有什么办法可以实现这一目标。 Otherwise I have to drop using Jetty. 否则,我必须放弃使用Jetty。

This is how I make my POST request: 这是我发出POST请求的方式:

Request request = httpClient.POST(url);
        //request.method(HttpMethod.POST);
        request.header(HttpHeader.ACCEPT, "application/json");
        request.header(HttpHeader.CONTENT_TYPE, "application/json");

        // Add basic auth header if credentials provided
        if (isCredsAvailable()) {
            String authString = username + ":" + password;
            byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
            String authStringEnc = "Basic " + new String(authEncBytes);
            request.header(HttpHeader.AUTHORIZATION, authStringEnc);
        }

        request(send);

Any help is appreciated! 任何帮助表示赞赏!

I was able to figure it out myself. 我自己就能弄清楚。 The solution was staring at me the whole time. 解决方案一直盯着我。

Added one line and everything worked: 添加了一行,一切正常:

    // Set request body
    request.content(new StringContentProvider(JSON_HERE), "application/json");

Hope it will be helpful to others as well. 希望对其他人也有帮助。

thanks you solve the problem which puzzles me for a long time. 谢谢您解决了困扰我很长时间的问题。

/*配置sslcontextfactory处理https请求*/
        SslContextFactory sslContextFactory = new SslContextFactory();
        HttpClient httpClient = new HttpClient(sslContextFactory);
        httpClient.setFollowRedirects(false);
        httpClient.start();

        /*配置post请求的url、body、以及请求头*/
        Request request =  httpClient.POST("https://a1.easemob.com/yinshenxia/yinshenxia/users");
        request.header(HttpHeader.CONTENT_TYPE, "application/json");
        request.content(new StringContentProvider("{\"username\":\"jliu\",\"password\":\"123456\"}","utf-8"));
//      request.param("username", "jliu");
//      request.param("password", "123456");
        ContentResponse response = request.send();
        String res = new String(response.getContent());
        System.out.println(res);
        httpClient.stop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetty Servlet:如何使用JSON正文将带有参数的GET请求作为POST请求转发? - Jetty servlet: How can I forward GET request with parameters as POST request with JSON body? 如何在 Jersey 客户端 HTTP POST 请求中发布原始 json 文本? - How to post raw json text in Jersey Client HTTP POST request? HTTP 带有请求正文的 POST - HTTP POST with request body 如何将 Serializable 编写为 http post 请求的正文? - How to write a Serializable as the body of an http post request? 具有标题和正文的Android异步Http客户端(Loopj)POST请求 - Android Asynchronous Http Client (Loopj) POST request with headers and body 如何将json添加到java中的http帖子的主体 - How to add json to the body of an http post in java 如何使用Jetty API客户端发送JSON请求? - How can I send a JSON request using Jetty API Client? 使用 jetty Http 客户端发送 Https 请求中的 NullPointerException - NullPointerException In Send Https Request With jetty Http Client 客户端发送的请求在语法上不正确-JSON POST正文中的类型复杂 - The request sent by the client was syntactically incorrect - Complicated type in JSON POST body 如何在 header 和 JSON 的正文中执行发布请求 - How to perform post request in header and body in JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM