0
votes

I have this method that makes a PATCH request to this URL:

https://username.visualstudio.com/DefaultCollection/_apis/wit/workitems/8?api-version=1.0

What does this method do: It updates an workItem`s System.Title, that has id 8, In Microsoft Visual Studio - Team Foundation Server

public void testPostVolley(String url) {
     /*Post data*/
    JSONArray jsonBody = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("op","replace");
        jsonObject.put("path","/fields/System.Title");
        jsonObject.put("value","Welcome to the jungle");
        jsonBody.put(jsonObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    Log.d("JSON Array " , String.valueOf(jsonBody));

    JsonArrayRequest postRequest = new JsonArrayRequest(Request.Method.PATCH, url,
            jsonBody,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d("Response Post Request", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //   Handle Error
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", MainRecyclerListView.TOKEN);
            headers.put("Content-Type", "application/json-patch+json");

            return headers;
        }
    };
    queue.add(postRequest);
}

}

I tried in POSTMAN with same Headers and same Body and it works, but in the Android app I`m getting this response:

07-06 13:05:49.253 2599-11381/ro.webivo.pockettask E/Volley: [209] BasicNetwork.performRequest: Unexpected response code 400 for https://username.visualstudio.com/DefaultCollection/_apis/wit/workitems/8?api-version=1.0

I Logged also the body I`m sending and is this one:

07-06 13:05:49.017 2599-2599/ro.webivo.pockettask D/JSON Array: [{"op":"replace","path":"/fields/System.Title","value":"Welcome to the jungle"}]

Official API for PATCH request is located here:

https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items

Could you please help me? Is this a Volley problem?

Thank you

1
There should be an error message in the response body when you get a "400 bad request" from VSTS Rest API, can you check and share the body message? And a similar question related to Android Volley: salesforce.stackexchange.com/questions/50382/…Eddie Chen - MSFT
The error message in the VolleyError response is the one I gave you. Do you know a way to customize the error response?dragosiv
So Ive tried to change Request.Method to POST and add different headers like : headers.put("X-HTTP-Method-Override", "PATCH");, and this time I was getting some json back from VSTS Rest services telling me they accept only Content-Type of application/json-patch+json. Ive changed it, but I`m getting again answer 400 and nothing else. I tried to log the error.networkresponse.data, but it comes null. Any idea?dragosiv

1 Answers

0
votes

After two days of searching I found a solution to my problem. I just changed the way I was doing the PATCH request. I changed from Volley to HttpClient and HttpCore libraries for this request.

JSONArray jsonBody = new JSONArray();
JSONObject jsonObject = new JSONObject();

try {
    jsonObject.put("op", "replace");
    jsonObject.put("path", "/fields/System.Title");
    jsonObject.put("value", title);
    jsonBody.put(jsonObject);
} catch (JSONException e) {
    e.printStackTrace();
}

HttpClient hc = new DefaultHttpClient();
String message;

HttpPost updateResultsPost = new HttpPost(url);


try {
    message = jsonBody.toString();


    updateResultsPost.setEntity(new StringEntity(message, "UTF8"));
    updateResultsPost.setHeader("Authorization", MainRecyclerListView.TOKEN);
    updateResultsPost.setHeader("X-HTTP-Method-Override", "PATCH");
    updateResultsPost.setHeader("Content-type", "application/json-patch+json");
    HttpResponse resp = hc.execute(updateResultsPost);
    if (resp != null && resp.getStatusLine().getStatusCode() == 200) {
        String responseBody = EntityUtils.toString(resp.getEntity());
        JSONObject jsonUpdatedResponse = new JSONObject(responseBody);
        Log.d("Status line", "" + String.valueOf(jsonUpdatedResponse));

    }

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {
    e.printStackTrace();
}

I hope this will help someone else.