LoginSignup
0
0

More than 1 year has passed since last update.

【WSL2】WSL2側からホストのlocalhostで立っているAPIサーバーのAPIをcURLで叩く方法

Last updated at Posted at 2022-04-20

概要

WSL2側からホストのlocalhostにアクセスする方法です。
WSL2側からホスト側に立てたAPIサーバーに、cURLでAPIを叩きたいと思った際にハマったので備忘録として残しておきます。

手順

1. nameserverのIPアドレスを取得する

ホスト側でipconfigを実行し、確認する方法

ホスト上のコマンドプロンプト等でipconfigを実行し、WSLIPv4アドレスを確認します。

イーサネット アダプター vEthernet (WSL):

   接続固有の DNS サフィックス . . . . .:
   リンクローカル IPv6 アドレス. . . . .: foo::bar:foo:bar:foo
   IPv4 アドレス . . . . . . . . . . . .: 172.***.***.1
   サブネット マスク . . . . . . . . . .: 255.255.240.0
   デフォルト ゲートウェイ . . . . . . .:

WSL2上側で確認する方法

/etc/resolv.confnameserverのIPアドレスを確認します。

$ cat /etc/resolv.conf

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.***.***.1

2. nameserverのIPをもとにWSL2側でcurlを使用し、APIを叩く

あとはWSL2側からcurlコマンドを使用し、APIを叩くだけです。

$ curl -X GET http://172.***.***.1/foo

参考文献

0
0
1

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
0
0