Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

18
22

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.

ExcelからREST呼び出しをする方法

Last updated at Posted at 2017-11-27

#VBAでRESTのリクエストを送信してみる
最近はいろいろなものをRESTで呼び出すケースが増えてきました。SOAPUIなどからRESTを呼び出すテストケースを作っていたのですが、テストケースが大量にあったためにExcelでテストケースを管理し、直接REST呼び出しをしてみることにしました。
##MSXML2.XMLHTTP.6.0を用いた呼び出し
単純なGETは以下のようなコードで簡単に呼び出すことが可能です。

http.vba
Dim objHTTP As Object
Set objHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
objHTTP.Open "GET", "http://test.com/index.htm, False
objHTTP.setRequestHeader "Authorization", "Basic EncodedUserNameAndPassword"
objHTTP.setRequestHeader "Content-Type", "text/plain"
objHTTP.send

##Proxyを設定する方法
IPアドレス制限があり、認証ありProxyを通す必要がある場合には、MSXML2.XMLHTTP.6.0ではなく、MSXML2.ServerXMLHTTP.6.0を用いて、setProxyCredentialsでProxyのユーザーIDとパスワードを指定します。
また、SSL証明書のエラーが発生する場合には、setOptionで、エラーを無視するように設定することで回避することが可能です。

Proxy.vba
Dim objHTTP As Object
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objHTTP.Open "GET", "https://test.com/index.htm", False
objHTTP.setRequestHeader "Authorization", "Basic EncodedUserNameAndPassword"
objHTTP.setRequestHeader "Content-Type", "text/plain"
objHTTP.setProxy 2, "proxyhost.com:9293"
objHTTP.setProxyCredentials "proxyUser", "ProxyPassword"
objHTTP.setOption 2, 13056
objHTTP.send

##まとめ
Excelから直接RESTリクエストを送信して、各種APIを呼び出すことができます。大量のテストケースを管理するのにExcelは便利ですし、戻りのJSONをパースしてマッチングさせることで、テストを自動化することが可能です。

18
22
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
Linked from these articles

Comments

No comments

Let's comment your feelings that are more than good

Being held Article posting campaign

18
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address