Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HttpRepl: A command-line tool for interacting with RESTful HTTP services

#1
HttpRepl: A command-line tool for interacting with RESTful HTTP services

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services.jpg" width="150" height="150" title="" alt="" /></div><div><div class="row justify-content-center">
<div class="col-md-4">
<div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services.jpg" width="58" height="58" alt="Angelos Petropoulos" class="avatar avatar-58 wp-user-avatar wp-user-avatar-58 alignnone photo"></p>
<p>Angelos</p>
</div>
</div>
</div>
<div class="entry-meta">
<p>July 31st, 2019</p>
</p></div>
<p><!-- .entry-meta --> </p>
<p>The ASP.NET team has built a command-line tool called <a href="https://github.com/aspnet/HttpRepl">HttpRepl</a>. It lets you browse and invoke HTTP services in a similar way to working with files and folders. You give it a starting point (a base URL) and then you can execute commands like “dir” and “cd” to navigate your way around the API:</p>
<pre><code>C:\&gt; dotnet httprepl http://localhost:65369/
(Disconnected)~ set base http://localhost:65369
Using swagger metadata from http://localhost:65369/swagger/v1/swagger.json http://localhost:65369/~ dir
. []
Fruits [get|post]
People [get|post] http://localhost:65369/~ cd People
/People [get|post] http://localhost:65369/People~ dir
. [get|post]
.. []
{id} [get]
</code></pre>
<p>Once you have identified the API you are interested in, you can use all the typical HTTP verbs against it. Here is an example of calling GET on http://localhost:65369/People as a continuation from before:</p>
<pre><code>http://localhost:65369/People~ get
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Wed, 24 Jul 2019 20:33:07 GMT
Server: Microsoft-IIS/10.0
Transfer-Encoding: chunked
X-Powered-By: ASP.NET [ { "id": 1, "name": "Scott Hunter" }, { "id": 0, "name": "Scott Hanselman" }, { "id": 2, "name": "Scott Guthrie" }
]
</code></pre>
<p>Right now HttpRepl is being <a href="https://www.nuget.org/packages/Microsoft.dotnet-httprepl">shipped</a> as a <a href="https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-global-tool">.NET Core Global Tool</a>, which means all you have to do to get it is run the following command on a machine with the .NET Core SDK installed:</p>
<pre><code>C:\&gt; dotnet tool install -g Microsoft.dotnet-httprepl --version “3.0.0-*”
</code></pre>
<p>The ASP.NET team built HttpRepl for the purpose of exploring and testing APIs. The idea was to make the experience of exploring and testing APIs through a command-line more convenient. What do you think about HttpRepl and what other uses do you envision for it? We would love to hear your opinion, please leave us a comment below or visit the project on <a href="https://github.com/aspnet/HttpRepl">GitHub</a>. And for those wondering, HttpRepl’s official ship date is expected to align with .NET Core 3.0 GA.</p>
<h2>Configure Visual Studio Code to launch HttpRepl on debug</h2>
<p>You can configure Visual Studio to launch HttpRepl when debugging (along with your web app) by creating a new launch configuration as follows:</p>
<pre><code>"version": "0.2.0", "compounds": [ { "name": ".NET Core REPL", "configurations": [ ".NET Core Launch (web)", "httprepl" ] } ], "configurations": [ { "name": "httprepl", "type": "coreclr", "request": "launch", "program": "dotnet", "args": ["httprepl", "http://localhost:5000"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "console": "integratedTerminal" }, { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/api.dll", "args": [], "cwd": "${workspaceFolder}", "stopAtEntry": false, // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser "serverReadyAction": { "action": "openExternally", "pattern": "^\\s*Now listening on:\\s+(https?://\\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }
</code></pre>
<h2>Configure Visual Studio for Windows to launch HttpRepl on F5</h2>
<p>You can configure Visual Studio to automatically launch HttpRepl when you F5 a project with the following simple steps:</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services.png" alt width="270" height="186" class="alignnone size-full wp-image-22353"></p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-1.png" alt width="480" height="470" class="alignnone size-full wp-image-22352"></p>
<p>The .exe for HttpRepl on Windows can be found in the following location:</p>
<pre><code>%USERPROFILE%\.dotnet\tools\dotnet-httprepl.exe
</code></pre>
<p>Don’t forget to select it from the menu after adding it:</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-2.png" alt width="459" height="235" class="alignnone size-full wp-image-22357"></p>
<p>Next time you F5 your project, Visual Studio will automatically launch HttpRepl with the appropriate base URL (same URL that would have been passed to a browser, controlled through <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2">launchsettings</a>):</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-3.png" alt width="896" height="226" class="alignnone size-full wp-image-22356"></p>
<p><em>Note: We are currently working on integrating HttpRepl into Visual Studio, which will give you an out-of-the box and more refined experience.</em></p>
<h2>Configure Visual Studio for Mac to launch HttpRepl as a Custom Tool</h2>
<p>In Visual Studio for Mac, you can configure a <a href="https://docs.microsoft.com/en-us/visualstudio/extensibility/internals/custom-tools?view=vs-2019">Custom Tool</a> to open a new Terminal window and start httprepl. To configure this, go to Tools&gt;Edit Custom Tools…</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-4.png" alt width="307" height="415" class="alignnone size-full wp-image-22355"></p>
<p>This will bring you to the External Tools dialog where you can add a new tool. To get started click the Add button to add a new tool. Here you will configure a new tool to launch a new Terminal instance and start the httprepl tool. Fill in the dialog with the following values.</p>
<ul>
<li>Title: dotnet httprepl </li>
<li>Command: osascript</li>
<li>Arguments: -e ‘tell application “Terminal” to activate’ -e ‘tell application “Terminal”<br />to do script “dotnet-httprepl”‘ </li>
<li>Working directory: ${ProjectDir}</li>
</ul>
<p>See the image below showing this new tool:</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-5.png" alt width="960" height="702" class="alignnone size-full wp-image-22354"></p>
<p>After clicking OK a new tool will appear in the Tools menu. To test your application with httprepl, start your application with Run&gt;Start Debugging (or Run&gt;Start without Debugging) and then start the httprepl with the new tool in the Tools menu:</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-6.png" alt width="283" height="335" class="alignnone size-full wp-image-22359"></p>
<p>When you invoke the tool a new Terminal window should appear in the foreground. From here you can set the base url to that of the api that you would like to test with set base. For example, see the image below that shows set base was executed and a get request will be executed next:</p>
<p><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services-7.png" alt width="905" height="579" class="alignnone size-full wp-image-22358"></p>
<h2>Give us feedback</h2>
<p>It’s not HTTPie, it’s not Curl, but it’s also not PostMan. It’s something that you run and stays running and its aware of its current context. We find this experience valuable, but ultimately what matters the most is what you think. Please let us know your opinion by leaving comments below or on <a href="https://github.com/aspnet/HttpRepl">GitHub</a>.</p>
<div class="authorinfoarea">
<div><img src="https://www.sickgaming.net/blog/wp-content/uploads/2019/08/httprepl-a-command-line-tool-for-interacting-with-restful-http-services.jpg" width="96" height="96" alt="Angelos Petropoulos" class="avatar avatar-96 wp-user-avatar wp-user-avatar-96 alignnone photo"></div>
</p></div>
</div>


https://www.sickgaming.net/blog/2019/07/...-services/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016