如何使用CloseableHttpResponse for Post请求在表单数据中传递参数

时间:2019-07-06 12:53:30

标签: api post automation httpresponse

如果尝试使用raw传递参数 “ {     “成功”:0,     “错误”:404,     “ error_message”:“无效的请求方法”,     “响应”:“错误” } 但是当在表单数据或x-www-form-urlencoded中传递参数时,邮递员会得到正确的响应

我已经看到了前面所有提出的问题,但我没有得到正确的输出,所以请解释一下。

使用closeablehttpResponse jar编写代码并执行,但是我得到的响应将是 {     “成功”:0,     “错误”:404,     “ error_message”:“无效的请求方法”,     “响应”:“错误” } 如何使用x-www-form-urlencoded或form-data而不是Raw string data传递参数。

public void PostApiTests() throws ClientProtocolException, IOException{
    iclapi=new ICL_API();

    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("appid", "F3561E632DF224DC2B1B9129FAD71");
    hashMap.put("Content-Type", "text/html");


    // Jackson Api
    ObjectMapper mapper = new ObjectMapper();
    parameters = new parameters("loginwithpass", "1038","1038","1234543");

    String jsonresp=mapper.writeValueAsString(parameters);
    System.out.println("Test Data"+jsonresp);

    closeablehttpResponse = iclapi.post(URL, jsonresp, hashMap);
    System.out.println("JSON Output - "+closeablehttpResponse);

    StatusCode = closeablehttpResponse.getStatusLine().getStatusCode();
    System.out.println("Status Code---->" + StatusCode);
    Assert.assertEquals(StatusCode, RESPONSE_STATUS_CODE_200,
            "Status is not 200");

    String body=EntityUtils.toString(closeablehttpResponse.getEntity(),"UTF-8");
    System.out.println("Body " + body);
    JSONObject jsonobj=new JSONObject(body);
    System.out.println("The response from API is " +jsonobj);

    String success=UtilMethod.getValueByJPath(jsonobj,"/error_message");
    System.out.println("Success ID "+success);

}

public CloseableHttpResponse post(String url,String entityString,HashMap<String,String> headerMap) throws ClientProtocolException, IOException{
    CloseableHttpClient httpClient=HttpClients.createDefault();
    HttpPost httppost=new HttpPost(url);

    httppost.setEntity(new StringEntity(entityString));

    for(Map.Entry<String, String> entry:headerMap.entrySet()){
        httppost.addHeader(entry.getKey(),entry.getValue());
    }
    CloseableHttpResponse closeablehttpResponse=httpClient.execute(httppost);
    return closeablehttpResponse;
    }

public static String getValueByJPath(JSONObject responsejson,String jpath){
    Object obj = responsejson;
    for(String s : jpath.split("/")) 
        if(!s.isEmpty()) 
            if(!(s.contains("[") || s.contains("]")))
                obj = ((JSONObject) obj).get(s);
            else if(s.contains("[") || s.contains("]"))
                obj = ((JSONArray) ((JSONObject) obj).get(s.split("\\[")[0])).get(Integer.parseInt(s.split("\\[")[1].replace("]", "")));
    return obj.toString();
}

public String getSuccess() {
    return success;
}


public void setSuccess(String success) {
    this.success = success;
}


public String getError() {
    return error;
}


public void setError(String error) {
    this.error = error;
}


public String getError_message() {
    return error_message;
}


public void setError_message(String error_message) {
    this.error_message = error_message;
}


public String getUser_id() {
    return user_id;
}


public void setUser_id(String user_id) {
    this.user_id = user_id;
}


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public String getPhone() {
    return phone;
}


public void setPhone(String phone) {
    this.phone = phone;
}


public String getRegion() {
    return Region;
}


public void setRegion(String region) {
    Region = region;
}


public String getDistrictcode() {
    return districtcode;
}


public void setDistrictcode(String districtcode) {
    this.districtcode = districtcode;
}


public String getDistrictname() {
    return districtname;
}


public void setDistrictname(String districtname) {
    this.districtname = districtname;
}


public String getAccess_token() {
    return access_token;
}


public void setAccess_token(String access_token) {
    this.access_token = access_token;
}


public parameters(String tag,String usercode,String password,String fcmid){
    this.tag=tag;
    this.usercode=usercode;
    this.password=password;
    this.fcmid=fcmid;
}


public String getTag() {
    return tag;
}


public void setTag(String tag) {
    this.tag = tag;
}


public String getUsercode() {
    return usercode;
}


public void setUsercode(String usercode) {
    this.usercode = usercode;
}


public String getPassword() {
    return password;
}


public void setPassword(String password) {
    this.password = password;
}


public String getFcmid() {
    return fcmid;
}


public void setFcmid(String fcmid) {
    this.fcmid = fcmid;
}

控制台输出     测试数据{“ tag”:“ loginwithpass”,“ usercode”:“ 1038”,“ password”:“ 1038”,“ fcmid”:“ 1234543”,“ success”:null,“ error”:null,“ error_message” :null,“ user_id”:null,“ name”:null,“ phone”:null,“ districtcode”:null,“ districtname”:null,“ access_token”:null,“ region”:null} JSON输出-HttpResponseProxy {HTTP / 1.1 200 OK [日期:星期六,2019年7月6日11:59:50 GMT,服务器:Apache,X-Powered-By:PHP / 5.5.38,到期日:1981年11月19日,星期四08: 52:00 GMT,缓存控制:无存储,无缓存,必须重新验证,max-age = 0,后检查= 0,预检查= 0,语法:无缓存,Set-Cookie:PHPSESSID = vcf0s368aom5b8hp0023qhp584;路径= /,升级:h2,h2c,连接:升级,保持活动状态,变化:接受编码,用户代理,保持活动状态:超时= 5,内容类型:application / json] org.apache.http。 client.entity.DecompressingEntity@79e4c792} 状态码----> 200 正文{“成功”:0,“错误”:404,“错误消息”:“无效的请求方法”,“响应”:“错误”} API的响应为{“ error_message”:“无效的请求方法”,“ Response”:“错误”,“成功”:0,“错误”:404}

0 个答案:

没有答案