Я использую JAVA, чтобы попытаться отправить HTTP-сообщение через Java в Insight (наше облако CMDB). По какой-то причине я получаю ошибку HTTP 400, и я не могу найти проблему, и я не могу узнать, как извлечь сообщение об ошибке из вызова HTTP.
package com.cmdbsync.app;
import java.io.IOException;
import com.google.gson.Gson;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
public class App
{
public static void main( final String[] args ) throws IOException, InterruptedException
{
String pc_data_json = "{" +
"\"objectTypeId\": \"90\"," +
"\"attributes\": [{ " +
"\"objectTypeAttributeId\": \"365\"," +
"\"objectAttributeValues\": [{" +
"\"value\": \"<PC name>\"" + // PC name
"}]" +
"}," +
"{" +
"\"objectTypeAttributeId\": \"548\"," +
"\"objectAttributeValues\": [{" +
"\"value\": \"<PC serial>\"" + // PC serial
"}]" +
"}," +
"{" +
"\"objectTypeAttributeId\": \"380\"," +
"\"objectAttributeValues\": [{" +
"\"value\": \"CM-283\"" +
"}]" +
"}," +
"{" +
"\"objectTypeAttributeId\": \"381\"," +
"\"objectAttributeValues\": [{" +
"\"value\": \"CM-284\"" +
"}]" +
"}," +
"{" +
"\"objectTypeAttributeId\": \"383\"," +
"\"objectAttributeValues\": [{" +
"\"value\": \"4\"" +
"}]" +
"}]" +
"}";
Gson gson = new Gson();
StringEntity entity = new StringEntity(gson.toJson(pc_data_json));
// HTTP calls
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost request = new HttpPost("https://insight-api.riada.io/rest/insight/1.0/object/create");
request.setHeader("Content-Type", "application/json");
request.setHeader("Authorization", "Bearer <api key>");
request.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(request);
System.out.println("Request");
Header[] headers = request.getAllHeaders();
for (Header header : headers) {
System.out.println("Key: " + header.getName() + ", value: " + header.getValue());
}
System.out.println("Response");
Header[] header = response.getAllHeaders();
for (Header head : header) {
System.out.println("Key : " + head.getName() + " , value : " + head.getValue());
}
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
} catch (Exception e) {
} finally {
System.out.println("Done");
}
}
}
Строка pc_data_ json должна быть правильной - причина того, как это сделано, заключается в том, что я должен импортировать ~ 700 компьютеров и, следовательно, выполнить это через al oop, читая файл .csv, и поэтому хочу иметь возможность изменить имя P C и серийный номер P C.
Это мой вывод при вызове программы
Picked up JAVA_TOOL_OPTIONS: -Djava.vendor="Sun Microsystems Inc."
Request
Key: Content-Type, value: application/json
Key: Authorization, value: Bearer <api key>
Response
Key : Server , value : nginx/1.14.0
Key : Date , value : Mon, 04 May 2020 08:43:21 GMT
Key : Content-Type , value : application/json
Key : Content-Length , value : 883
Key : Connection , value : keep-alive
Key : X-Content-Type-Options , value : nosniff
Key : X-XSS-Protection , value : 1; mode=block
Key : Cache-Control , value : no-cache, no-store, max-age=0, must-revalidate
Key : Pragma , value : no-cache
Key : Expires , value : 0
Done
Exception in thread "main" java.lang.AssertionError:
Expected: <200>
but: was <400>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at com.cmdbsync.app.App.main(App.java:83)
Надеюсь, некоторые мастера здесь могут помочь мне!