1
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ServiceNowでJavaのHttpClientを操作する

Last updated at Posted at 2018-04-27

作るもの

ServiceNowのServer SideのScriptでJavaのHttpClientを操作します

FireShot Screen Capture #013 - 'Incident Ticket System' - dev48240_service-now_com_nav_to_do_uri=_sys_scripts_do.png

Scripts - Backgroundで試す

ServiceNowには[System Web Service] - [▼Outbound] - [SOAP Message], [System Web Service] - [▼Outbound] - [REST Message]とServiceNowから外部サイトにHTTP Requestを送る機能が用意されているのですが

[ServiceNowでHTTP Request/Responseを実行してScriptへの組み込み方を確認する]
https://qiita.com/20_percent_cooler/items/468edc9a1fbe51ca179a

外部サイトのWeb ServerにおいてHttp ResponceのHeaderにSet-Cookieを指定された場合に上記機能だけで一旦受け取って次のHTTP Requestに設定するといったことが難儀だったので他に方法が無いか検索したところ、以下の情報を見つけました

[HTTP POST to external url - Developer Community - ServiceNow Community](
https://community.servicenow.com/community?id=community_question&sys_id=05b01f69dbdcdbc01dcaf3231f961973

This should get you started. It is basically just the Java HttpClient library so the javadocs have been helpful to us in figuring out obscure details.

var client = new Packages.org.apache.commons.httpclient.HttpClient();
var post = new Packages.org.apache.commons.httpclient.methods.PostMethod("http://www.exampledestination.com/page_to_post_to.html");
post.addParameter("parameter_name", "parameter value");
var returnCode = client.executeMethod(post);
var output = "" + post.getResponseBodyAsString();

上記コードのurlの値を http://example.com に変更して最終行に画面にプリントする文を追加して

var client = new Packages.org.apache.commons.httpclient.HttpClient();
var post = new Packages.org.apache.commons.httpclient.methods.PostMethod("http://www.example.com");
post.addParameter("parameter_name", "parameter value");
var returnCode = client.executeMethod(post);
var output = "" + post.getResponseBodyAsString();
gs.print(output);

[Scripts - BackGround]のRun script(JavaScript execute on server)にコピペして

FireShot Screen Capture #014 - 'Incident Ticket System' - dev48240_service-now_com_nav_to_do_uri=_sys_scripts_do.png

[Run script]をクリックして実行して成功しました

FireShot Screen Capture #013 - 'Incident Ticket System' - dev48240_service-now_com_nav_to_do_uri=_sys_scripts_do.png

JavaのHttpClientが使えるのであればSet-Cookieをそのまま受け取れるので認証認可処理があるWeb Serverとおしゃべりする際など便利そうです

アプリケーションに組み込む場合には上記コードをServer SideのScript IncludeにClient Callableで実装してClientからはUI Actionのボタンから読み込んで実行といった感じでできそうです

1
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?