如何使用Jetty http客户端将json作为请求主体发布?

lka*_*las 2 java post jetty

我坚持在POST请求体中添加JSON.没有任何方法可以设置请求的主体.

有什么方法可以实现这一点.否则我不得不放弃使用Jetty.

这是我发出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);
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

lka*_*las 6

我能够自己解决这个问题.解决方案一直盯着我看.

添加了一行,一切正常:

    // Set request body
    request.content(new StringContentProvider(JSON_HERE), "application/json");
Run Code Online (Sandbox Code Playgroud)

希望它对其他人也有帮助.