HTTP Post请求,以列表作为数据参数

时间:2018-12-06 04:23:51

标签: java json http automated-tests web-api-testing

我正在尝试编写一个自动化测试用例来验证服务,该服务接受列表作为http正文/数据参数。当我通过浏览器执行此操作时,其工作正常(以下是成功请求的卷曲)

curl 'https://mytest.org.com/admin/addValue' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:63.0) Gecko/20100101 Firefox/63.0' -H 'Accept: application/json' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/json' -H 'CSRF_TOKEN: 18243bk3lu8hakfosidu56b40de4356dkm3u643j0e2ldcsc85si' -H 'Connection: keep-alive' -H 'Cookie: dslang=US-EN; ac9-at=Nq41QacCjw2tdOUBw6aKlq/yybAARv9GvFe/F; geo=US; cpl=Dsuy09ercyiX9MfjSvjDKPq45Qgqe+0gp81c4DFb000=' --data '[{"label":"MultiValueId","value":"California","instanceId":2049}]'

虽然,当我尝试使用自动化套件时,值却没有按预期保存-

        HttpPost httppost = new HttpPost("https://mytest.org.com/admin/addValue");
        httppost.addHeader(HttpHeaders.CONNECTION, "keep-alive");
        httppost.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7");
        httppost.addHeader(HttpHeaders.ACCEPT, "application/json");
        httppost.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip,deflate,br");
        httppost.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "en-us");
        httppost.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httppost.addHeader("CSRF_TOKEN", MyConstant.CSRF_TOKEN);
        httppost.addHeader("Cookie", MyConstant.cookie);

        ArrayList<NameValuePair> valParameters=new ArrayList<NameValuePair>();
        valParameters.add(new BasicNameValuePair("label", "MultiValueId"));
        valParameters.add(new BasicNameValuePair("value", "California"));
        valParameters.add(new BasicNameValuePair("instanceId", "2049"));

        List postParameters=new ArrayList<Object>();
        postParameters.add(valParameters);
        httppost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
        httppost.setHeader("Content-type", "application/json");

        System.out.println(httppost.toString());
        HttpResponse resp = HttpsSSLClient.createSSLInsecureClient().execute(httppost);
        System.out.println("Response --> " + resp.toString()+resp.getEntity());
        System.out.println("Response Entity--> " + resp.getEntity());

下面是http post数据参数-

  

[{“ label”:“ MultiValueId”,“ value”:“ DEF”,“ instanceId”:2049},{“ label”:“ MultiValueId”,“ value”:“ GHI”,“ instanceId”:2049 }]

此外,我知道以下是在应用程序端编写服务的方式,即它接受list作为参数-

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Path("/addValue")

  public ValueUploadResponse addLabelValue(List<GenericUXData> fieldList)  {

        ValueUploadResponse response = new ValueUploadResponse();
        List<GenericUXData> savedValues = new ArrayList<GenericUXData>();
        List<GenericUXData> errorValues = new ArrayList<GenericUXData>();
        if (fieldList == null || fieldList.size() < 1) {
              response.setErrorMessage("No Field List Found");
              return response;
        }
......
......
......

请建议如何实现。

0 个答案:

没有答案