Not able to send wipe request with json parameter
We see 400 errors while using java SDK to send wipe request. After trouble shooting in microsoftgraph/msgraph-sdk-java#796, We found this wipe REST API has issues with json request.
I used the following code to send the same request via okhttpclient
1: using JSON and
2 using form_urlencoded in body.
JSON request get 400 error code, form_urlencoded get expected 204. Although https://learn.microsoft.com/en-us/graph/api/intune-devices-manageddevice-wipe?view=graph-rest-beta says the api accept json.
private String graphBase = "https://graph.microsoft.com/v1.0";
public void okhttpwipe() throws Exception {
MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
String url = graphBase + "/deviceManagement/managedDevices/d0036e3d-144c-401f-b394-a65dcc5acfad/microsoft.graph.wipe";
//RequestBody body = new FormBody.Builder()
// .add("keepEnrollmentData","true").add("keepUserData","true").add("macOsUnlockCode","").build();
RequestBody body = RequestBody.create(JSON,"{\"keepEnrollmentData\":true,\"keepUserData\":true,\"macOsUnlockCode\":\"\"}");
Request request = new Request.Builder()
.url(url)
.addHeader("User-Agent", "OkHttp Bot")
.post(body)
.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// Get response body
System.out.println(response.body().string());
}
}
json request output:
2021. 06.16 14:22:32.702 [main] INFO c.m.m.u.i.d.i.LogInterceptor - Request{method=POST, url=https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/d0036e3d-144c-401f-b394-a65dcc5acfad/microsoft.graph.wipe, headers=[User-Agent:OkHttp Bot, SdkVersion:graph-java-core/v2.0.0 (featureUsage=0), java/1.8.0_192, client-request-id:cf028f15-09e7-4d17-95fd-87a526567f5e, Authorization:Bearer ..., Content-Type:application/json; charset=utf-8, Content-Length:68, Host:graph.microsoft.com, Connection:Keep-Alive, Accept-Encoding:gzip], tags={class com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions=com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions@26f96b85}}
2021. 06.16 14:22:32.702 [main] INFO c.m.m.u.i.d.i.LogInterceptor - body={"keepEnrollmentData":true,"keepUserData":true,"macOsUnlockCode":""}
2021. 06.16 14:22:33.222 [main] INFO c.m.m.u.i.d.i.LogInterceptor - Response{protocol=http/1.1, code=400, message=Bad Request, url=https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/d0036e3d-144c-401f-b394-a65dcc5acfad/microsoft.graph.wipe}
form request output:
2021. 06.16 14:21:09.923 [main] INFO c.m.m.u.i.d.i.LogInterceptor - Request{method=POST, url=https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/d0036e3d-144c-401f-b394-a65dcc5acfad/microsoft.graph.wipe, headers=[User-Agent:OkHttp Bot, SdkVersion:graph-java-core/v2.0.0 (featureUsage=0), java/1.8.0_192, client-request-id:973d540f-e9d6-4210-a405-fa482d767249, Authorization:Bearer ..., Content-Type:application/x-www-form-urlencoded, Content-Length:58, Host:graph.microsoft.com, Connection:Keep-Alive, Accept-Encoding:gzip], tags={class com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions=com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions@78e89bfe}}
2021. 06.16 14:21:09.924 [main] INFO c.m.m.u.i.d.i.LogInterceptor - body=keepEnrollmentData=true&keepUserData=true&macOsUnlockCode=
2021. 06.16 14:21:10.762 [main] INFO c.m.m.u.i.d.i.LogInterceptor - Response{protocol=http/1.1, code=204, message=No Content, url=https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/d0036e3d-144c-401f-b394-a65dcc5acfad/microsoft.graph.wipe}