尝试在清单中执行 js 脚本以请求 privatte API 和 post 请求

Try to execute js script in manifest to ask privatte API with post request

我尝试在我的 jps 中使用 js 脚本来测试个人 APi 的凭据。这个想法是 return 如果凭据为假,则在 jps 中显示一个错误代码。在我的电脑上,我的 js 脚本工作正常,但是当我尝试用它启动我的 jps 时,出现 javascript 错误。

我的老师:

onInstall:

- script [*]: https://github.com/user/project/blob/master/script.js responses: 401: type: error message: bad credentials

我的 js 脚本:


    const https = require('https')


    var name = "some-name"
    var password = "some-password"```



    const data = JSON.stringify({
      "auth": {
          "identity": {
              "methods": [
                  "password"
              ],
              "password": {
                  "user": {
                      "domain": {
                          "id": "default"
                      },
                      "name": name,
                      "password": password
                  }
              }
          },
          "scope": {
              "project": {
                  "domain": {
                      "id": "default"
                  },
                  "name": "some-name"
              }
          }
      }
    })
    const options = {
      hostname: 'mYapi.com',
      port: 443,
      path: 'mypath',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
      }
    }

    var req = https.request(options, (res) => {
      console.log(`statusCode: ${res.statusCode}`)
      console.log(res.statusCode)
      return res.statusCode;

    })

    req.on('error', (error) => {
      console.error(error)

    })

    req.write(data)
    req.end()


我在控制台中收到此错误:

ERROR: script.response: {"result":1704,"line":50,"response":null,"source":"hx-core","time":122,"error":"org.mozilla.javascript.EvaluatorException: syntax error"}

并且我尝试了很多不同的脚本来执行此 post 请求 ----> 在我的计算机上工作(api 如果凭据良好则发送结果 201,否则发送结果 401), -----> 在 jelastic 清单中不起作用。

所以请你解释一下我如何在 Jelastic 清单(js 调用脚本)中的 API 上使用 json 执行 post 请求。我提前谢谢你 !

由 "script" 操作执行的代码在 JVM 上运行,因此它允许您连接和使用 Java 库。 要执行 POST 请求并确定输出代码的状态,您可以使用 Commons HttpClient。 请参阅下面的示例。

type: install
name: HttpClient Post

onInstall:
- script: |
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.StringRequestEntity;

    var client = new HttpClient();

    var name = "some-name";
    var password = "some-password";

    var requestEntity = new StringRequestEntity(toJSON({
        "auth": {
            "identity": {
                "methods": [
                    "password"
                ],
                "password": {
                    "user": {
                        "domain": {
                            "id": "default"
                        },
                        "name": name,
                        "password": password
                    }
                }
            },
            "scope": {
                "project": {
                    "domain": {
                        "id": "default"
                    },
                    "name": "some-name"
                }
            }
        }
    }), "application/json", "UTF-8");


    var post = new PostMethod("https://example.com/");

    post.setRequestEntity(requestEntity);

    var status = client.executeMethod(post);
    post.releaseConnection();

    if (status == HttpStatus.SC_CREATED) { // 201
        return { type : "success" };    
    } else if (status == HttpStatus.SC_UNAUTHORIZED) { // 401
        return { type: "error", message: "bad credentials" };
    }

    return { type: "error", message: "unknown error" };

此外,您可以在 Jelastic JPS Collection 存储库中找到许多有用的示例和信息。

也许下一个脚本会对您有用: https://github.com/jelastic-jps/git-push-deploy/blob/master/scripts/add-web-hook.cs

最后一件事,如果您不需要确切的 HTTP 状态,您可以使用集成的 "Transport" class。

import com.hivext.api.core.utils.Transport;

var name = "some-name";
var password = "some-password";

var data = toJSON({
  "auth": {
    "identity": {
      "methods": [
        "password"
      ],
      "password": {
        "user": {
          "domain": {
            "id": "default"
          },
          "name": name,
          "password": password
        }
      }
    },
    "scope": {
      "project": {
        "domain": {
          "id": "default"
        },
        "name": "some-name"
      }
    }
  }
});

try {
    new Transport().post("https://example.com/", data, {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    });    

    return { type: "success" };
} catch (e) {
    return {
        type: "error",
        message: "unknown error: " + e
    };
}