Search code examples
javasharepointhttpclientfiddlerhttp-status-code-400

400 Bad Request Invalid Verb SharePoint Rest Api Java


I am trying to post a request to SharePoint REST interface. I am trying to update a column value. I am able to successfully do this when I post using Fiddler (Response: 204) . When I send using Apache HttpClient I get 400 Bad Request - Invalid Verb.

As a side note, when I try to use Fiddler as a proxy (localhost, Port 8888) for this program, the request goes successfully. But when I again remove the proxy and try it is unsuccessful. Am I doing something wrong with my Java code?

Is this the way to send a JSON string through HTTP post ?

private static final String API_ENDPOINT = "/_api/web";
    private static final String CONTEXT_INFO_ENDPOINT = "/_api/contextinfo";
    private static final String APPLICATION_JSON = "application/json;odata=verbose";
    private static final String IF_MATCH = "If-Match";
    private static final String X_HTTP_Method = "X-HTTP-Method";
    private static final String MERGE = "MERGE";
    private static final String X_RequestDigest = "X-RequestDigest";
    private static final String METADATA = "__metadata";

    public static boolean setFieldValue(CloseableHttpClient httpClient, String siteURL, String serverRelativeURL, String fieldName, String fieldValue, String fieldType) {

    CloseableHttpResponse response = null;

    try {
        URI siteURI = new URI(siteURL);
        URI postURI = new URIBuilder(siteURI)
                .setPath(siteURI.getPath() + API_ENDPOINT + "/GetFileByServerRelativeUrl('" + serverRelativeURL + "')/ListItemAllFields")
                .build();
        HttpPost postRequest = new HttpPost(postURI);

        postRequest.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON );
        postRequest.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
        postRequest.addHeader(HttpHeaders.HOST, siteURI.getHost());
        postRequest.addHeader(IF_MATCH, "*");
        postRequest.addHeader(X_HTTP_Method, MERGE);

        String formDigestValue = getFormDigestValue(httpClient, siteURI);
        if (StringUtils.isBlank(formDigestValue)) {
            logger.error("FORM DIGEST VALUE IS = " + formDigestValue);
            return false;
        }
        postRequest.addHeader(X_RequestDigest, formDigestValue);

        JSONObject type = new JSONObject();
        type.put("type", fieldType);

        JSONObject jsonBody = new JSONObject();
        jsonBody.put(METADATA , type);
        jsonBody.put(fieldName, fieldValue);


        logger.debug("setFieldValue(): " + jsonBody.toString());
        postRequest.setEntity(new StringEntity(jsonBody.toString()));
        //String jsonString = "{'__metadata':{'type':'SP.Data.NextlabsDLItem'},'TextHeader':'Java1'}";
        //JSONObject jsonObject = new JSONObject(jsonString);
        //logger.debug(jsonObject);
        //postRequest.setEntity(new ByteArrayEntity(jsonBody.toString().getBytes(), ContentType.APPLICATION_JSON));
        logger.debug("setFieldValue(): " + postRequest.getRequestLine());

        response = httpClient.execute(postRequest);

        logger.debug("setFieldValue(): " + response.getStatusLine());

        int status = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
        int cp;
        StringBuilder sb = new StringBuilder();
        while ((cp = bufferedReader.read()) != -1) {
            sb.append((char) cp);
        }
        logger.debug("String Response......." + sb);
        bufferedReader.close();
        if (status >= 200 && status < 300) {
            return true;
        } else {
            logger.error("setFieldValue(): " + response.getStatusLine().toString().toUpperCase());
        }

    } catch (URISyntaxException | IOException e) {
        logger.error("setFieldValue(): " + e.getMessage(), e);
    } finally {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;

}

Solution

  • Sorry for posting my own answer but finally found the problem.

    The mistake was ExpectContinuedFlag was enabled in apache HTTPClient.

    I changed the following in HTTP initialization and now it works!

        RequestConfig requestConfig = RequestConfig.custom()
                .setExpectContinueEnabled(***false***)
                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                .setProxyPreferredAuthSchemes(Collections.singletonList(AuthSchemes.BASIC))
                .build();
    
        return HttpClients.custom()
                .setConnectionManager(connManager)
                .setDefaultCredentialsProvider(creds)
                .setDefaultRequestConfig(requestConfig)
                .build();