Tôi đang cố gọi một cuộc gọi còn lại bằng cách sử dụng phần còn lại được đảm bảo. API của tôi chấp nhận, "application/json"
làm loại nội dung và tôi cần phải đặt trong cuộc gọi. Tôi đặt loại nội dung như được đề cập bên dưới.đặt loại nội dung ở trạng thái yên tâm
Lựa chọn 1
Response resp1 = given().log().all().header("Content-Type","application/json")
.body(inputPayLoad).when().post(addUserUrl);
System.out.println("Status code - " +resp1.getStatusCode());
Lựa chọn 2
Response resp1 = given().log().all().contentType("application/json")
.body(inputPayLoad).when().post(addUserUrl);
Câu trả lời tôi nhận được là "415" (chỉ ra rằng "loại phương tiện không được hỗ trợ").
Tôi đã thử gọi cùng một api bằng cách sử dụng mã java đơn giản và nó hoạt động. Đối với một số lý do bí ẩn, tôi cudn't làm cho nó làm việc thông qua RA.
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(addUserUrl);
StringEntity input = new StringEntity(inputPayLoad);
input.setContentType("application/json");
post.setEntity(input);
HttpResponse response = client.execute(post);
System.out.println(response.getEntity().getContent());
/*
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("Output -- " +line);
}
Bạn có thể so sánh tiêu đề yêu cầu từ 2 ví dụ đầu tiên của mình để yêu cầu tiêu đề từ ví dụ sau không? – spg
Tùy chọn 1: Phương thức yêu cầu: \t POST Đường dẫn yêu cầu: \t http: //10.75.43.46: 7001/supplierapp chiến tranh/pim/adduser Yêu cầu params: \t Query params: \t params Mẫu: \t params Đường dẫn: \t Headers: \t \t Content-Type = application/json Cookies: \t \t phương án 2: Yêu cầu phương pháp: \t POST Yêu cầu đường dẫn: \t http://10.75.43.46:7001/supplierapp-war/pim/addUser Yêu cầu params: \t params truy vấn: \t Mẫu params: \t params Đường dẫn: \t Headers: \t \t Content-Type = application/json Cookies: \t \t –
TechRookie
Yêu cầu được gửi bởi HttpClient: Content Type: trang nội dung Loại: application/json Độ dài nội dung: 203 Nội dung: [email protected] nội dung Mã hóa: null Lưu ý: Tôi đã sử dụng đoạn mã sau để lấy thông tin tiêu đề từ httpClient. System.out.println ("Loại nội dung:" + input.getContentType()); System.out.println ("Độ dài nội dung:" + input.getContentLength()); System.out.println ("Nội dung:" + input.getContent()); System.out.println ("content Encoding:" + input.getContentEncoding()); – TechRookie