[英]Error on POST request with Jersey to API, works fine with POSTMAN
我用Laravel 5開發了一個API RESTful服務(后端),我用POSTMAN測試了它。 有幾種資源可以獲取有關GET和POST請求的信息,它們可以與POSTMAN一起使用。 我開始使用帶有Jersey的Java客戶端測試API。 GET方法適用於這樣的代碼,響應是json,我與Jackson解析。
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(ClientUrl);
Invocation.Builder invocationBuilder =
webTarget.request(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
Response mResponse = invocationBuilder.get();
我要測試的下一個方法是登錄,使用POST方法。 使用POSTMAN我在標題中使用鍵“Content-Type”+值“application / x-www-form-urlencoded”並在正文中我發送一個帶有此信息的Json
{"email":"xxxxxx@gmail.com", "password":"xxxxxxxxxx"}
要在Jersey中使用POST方法,請使用下一個代碼
Client client = ClientBuilder.newClient();
Form mForm = new Form();
mForm.param("email", email);
mForm.param("password", password);
WebTarget target = client.target(Utils.URL_LOGIN);
//As stated in the documentation, the APPLICATION_FORM_URLENCODED =
//"application/x-www-form-urlencoded"
Builder request = target.request(MediaType.APPLICATION_FORM_URLENCODED);
//I print the request to verify the request
print(request.toString());
Response response = request.post(Entity.form(mForm));
//I print the response to verify the response received and also the code
print("Response: " + response.toString());
print("Code: " + response.getStatus());
String result = response.readEntity(String.class);
response.close();
print(result);
當我打印request.toString()時,我得到了這個
org.glassfish.jersey.client.JerseyInvocation$Builder@65e579dc
打印響應時
Response: InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://www.dance-world.com/api/login, status=200, reason=OK}}
收到的狀態是200,但是當我打印response.readEntity時,我得到了我在后端使用的響應,當服務器沒有收到請求信息時。
{"code":400,"status":"error","message":"No data attached to the request"}
當我使用POSTMAN並且結果是200時,成功地我將Token作為String接收。
我無法確定我的錯誤是什么,因為我正在遵循文檔中的代碼以及使用JAX-RS 2.0的RESTful Java一書
我也嘗試使用Unirest,因為它比澤西島更容易使用。 我輸入下面的代碼
try {
HttpRequestWithBody requestwithBody = Unirest.post(url)
.header("Content-Type", "application/json")
.header("accept", "application/json");
RequestBodyEntity requestBodyEntity = requestwithBody.body(input);
response = requestBodyEntity.asJson();
} catch (UnirestException e) {
e.printStackTrace();
} finally {
print("Status: " + String.valueOf(response.getStatus()));
print("Body: " + String.valueOf(response.getBody()));
}
本地服務器沒有收到json正文,我收到了來自服務器的相同響應
{"code":400,"status":"error","message":"No data attached to the request"}
我找到了原因。 我根據Laravel的課程開發了后端,在該課程中,函數具有下一個定義
public function getByCountry(Request $request) {
//Recoger los datos por post
$json = $request->input('json', null);
$params = json_decode($json);
$params_array = json_decode($json, true);
...
}
並且后端需要x-www-urlencoded,在POSTMAN中我以該格式發送請求(POST Post {x-www-urlencoded} with Postman) 。 因此,當我嘗試使用JSON發出POST請求時,使用Jersey或Unirest請求沒有所需的格式,這就是我在后端沒有收到te請求信息的情況下配置消息的原因。 這是帶有原始Json的POSTMAN請求, 這是后端的響應 ,表示沒有收到任何數據。
我解決它接受json請求的方式是使用下一個結構更改后端的代碼:
public function TestForPost(Request $request) {
//Verify if the $request is a json
$input = $request->json()->all(); //read json in request
if (!empty($input)) {
...
}
通過這個改變,我向Json發送請求,后端接受它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.