Okhttp를 사용하여 put 빈 RequestBody 실행

특수한 원인으로 인해 클라이언트는put 요청을 해야 하지만 어떤 매개 변수도 필요하지 않습니다. 구체적인 매개 변수는 url에 있습니다. 그러나 Okhttp는 Put에서 RequestBody 매개 변수의 원본 코드를 전달해야 합니다.
public Builder put(RequestBody body) {
      return method("PUT", body);
    }

...


public Builder method(String method, RequestBody body) {
      if (method == null) throw new NullPointerException("method == null");
      if (method.length() == 0) throw new IllegalArgumentException("method.length() == 0");
      if (body != null && !HttpMethod.permitsRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must not have a request body.");
      }
      if (body == null && HttpMethod.requiresRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must have a request body.");
      }
      this.method = method;
      this.body = body;
      return this;
    }

패스하지 않고 폴백 이상을 볼 수 있습니다. 이 경우 다음과 같이 처리할 수 있습니다.
RequestBody requestBody = RequestBody.create(null, new byte[]{});
Request request = new Request.Builder()
        .url(url)
        .header("Content-Type",contentType)
        .put(requestBody)
        .build();

좋은 웹페이지 즐겨찾기