OpenSTF API POST remoteConnect

时间:2017-06-21 11:59:44

标签: java rest post apache-httpclient-4.x apache-httpcomponents

OpenSTf有API POST / api / v1 / user / devices / {serial} / remoteConnect

我需要Apache http POST发送请求并传递序列。

      HttpPost request = new HttpPost("/api/v1/user/devices/"+ serial +"/remoteConnect");
HttpPost request1 = new HttpPost("/api/v1/user/devices/remoteConnect");

 request.setConfig(config);

                ArrayList<BasicNameValuePair> postParameters = new ArrayList<>();
                postParameters.add(new BasicNameValuePair("serial", serial));

                request.setEntity(new UrlEncodedFormEntity(postParameters));

请求和request1不起作用,我不明白怎么写 新HttpPost中的API URL?

1 个答案:

答案 0 :(得分:2)

我没有看到您发送访问令牌?可能是您的请求失败的原因。可以在OpenSTF的设置部分中创建访问令牌。

可能导致请求失败的另一个问题是您不首先将设备设置为用户。

请注意,这是我的解决方案,我正在使用RestAssured Library来执行POST请求。

 String AccessToken = "Bearer e334457w44423e2342b6e5b8e5e0e723423488487b5cfe3";
 String BaseUrl = "http://myopenstfapiurl.com/api/v1";
 String deviceserial = "0123456789ABCDEF";

public void SetADeviceToUser() {
    RestAssured.baseURI = BaseUrl;
    RestAssured.given().contentType(ContentType.JSON).header("Authorization", AccessToken)
        .body("{\"serial\": \"" + deviceserial + "\",\"timeout\": 0}").post("/user/devices")
        .asString();
  }

  public String GetRemoteUrlForDevice() {
    RestAssured.baseURI = BaseUrl;
    String response = RestAssured.given().contentType(ContentType.JSON)
        .header("Authorization", AccessToken)
        .post("/user/devices/" + deviceserial + "/remoteConnect").asString();

    System.out.println(response);
    //Return the remoteConnectURL which will be used to connect to the device. 
    return JsonPath.parse(response).read("$.remoteConnectUrl");
  }