HttpRequest - POST request, response in C# with httprepl

Markus Freitag 3,791 Reputation points
2021-05-14T15:24:07.617+00:00

Hello,

I just want to send something and get the answer.
What is not clear to me, I need to install Swagger?
What do I want?
I want to send something JSON object and return the response.
Very much in a file.

Response from server  - JSON content inside the text file.
    if(response.empty())
    {
        j_response["foodData"]["code"] = -1;
        j_response["foodData"]["message"] = response;
        j_response["data"] = "";
    }

I'm looking for a good short instruction, step by step, what need.
httprepl-a-command-line-tool-for-interacting-with-restful-http-services-d803e5751408

My attempts, unfortunately it does not work.

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>httprepl https://localhost:5001
Welcome to HttpRepl 5.0!
------------------------

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. The data is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_HTTPREPL_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about HttpRepl telemetry: https://aka.ms/httprepl-telemetry
Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

(Disconnected)> connect https://localhost:5001
Using a base address of https://localhost:5001/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

https://localhost:5001/> pref get
colors.json=Green
colors.json.arrayBrace=BoldCyan
colors.json.comma=BoldYellow
colors.json.name=BoldMagenta
colors.json.nameSeparator=BoldWhite
colors.json.objectBrace=Cyan
colors.protocol=BoldGreen
colors.status=BoldYellow

https://localhost:5001/>  post -h Content-Type=application/json
The default editor must be configured using the command `pref set editor.command.default "{commandLine}"`.

https://localhost:5001/> pref set httpClient.useDefaultCredentials true

https://localhost:5001/> echo on
Request echoing is on

https://localhost:5001/> post
The default editor must be configured using the command `pref set editor.command.default "{commandLine}"`.

https://localhost:5001/> pref set editor.command.default
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.HttpRepl.Commands.PrefCommand.IsVSCode(String path) in /_/src/Microsoft.HttpRepl/Commands/PrefCommand.cs:line 279
   at Microsoft.HttpRepl.Commands.PrefCommand.SetSetting(IShellState shellState, HttpState programState, DefaultCommandInput`1 commandInput) in /_/src/Microsoft.HttpRepl/Commands/PrefCommand.cs:line 188
   at Microsoft.HttpRepl.Commands.PrefCommand.ExecuteAsync(IShellState shellState, HttpState programState, DefaultCommandInput`1 commandInput, ICoreParseResult parseResult, CancellationToken cancellationToken) in /_/src/Microsoft.HttpRepl/Commands/PrefCommand.cs:line 164
   at Microsoft.Repl.Commanding.CommandWithStructuredInputBase`2.ExecuteAsync(IShellState shellState, TProgramState programState, TParseResult parseResult, CancellationToken cancellationToken) in /_/src/Microsoft.Repl/Commanding/CommandWithStructuredInputBase.cs:line 213
   at Microsoft.HttpRepl.Telemetry.TelemetryCommandWrapper.ExecuteAsync(IShellState shellState, HttpState programState, ICoreParseResult parseResult, CancellationToken cancellationToken) in /_/src/Microsoft.HttpRepl/Telemetry/TelemetryCommandWrapper.cs:line 39
   at Microsoft.Repl.Commanding.DefaultCommandDispatcher`2.ExecuteCommandInternalAsync(IShellState shellState, CancellationToken cancellationToken) in /_/src/Microsoft.Repl/Commanding/DefaultCommandDispatcher.cs:line 154
   at Microsoft.Repl.Commanding.DefaultCommandDispatcher`2.ExecuteCommandAsync(IShellState shellState, CancellationToken cancellationToken) in /_/src/Microsoft.Repl/Commanding/DefaultCommandDispatcher.cs:line 117

https://localhost:5001/> pref set editor.command.default "C:\Temp"

https://localhost:5001/> post
The specified default editor path, "C:\Temp", does not exist.

https://localhost:5001/> pref set editor.command.default "C:\Temp\test.txt"

https://localhost:5001/> post
System.ComponentModel.Win32Exception (193): The specified executable is not a valid application for this OS platform.
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Microsoft.HttpRepl.Commands.BaseHttpCommand.HandleRequiresBody(DefaultCommandInput`1 commandInput, IShellState shellState, HttpState programState, HttpRequestMessage request, Dictionary`2 requestHeaders) in /_/src/Microsoft.HttpRepl/Commands/BaseHttpCommand.cs:line 286
   at Microsoft.HttpRepl.Commands.BaseHttpCommand.ExecuteAsync(IShellState shellState, HttpState programState, DefaultCommandInput`1 commandInput, ICoreParseResult parseResult, CancellationToken cancellationToken) in /_/src/Microsoft.HttpRepl/Commands/BaseHttpCommand.cs:line 139
   at Microsoft.Repl.Commanding.DefaultCommandDispatcher`2.ExecuteCommandInternalAsync(IShellState shellState, CancellationToken cancellationToken) in /_/src/Microsoft.Repl/Commanding/DefaultCommandDispatcher.cs:line 154
   at Microsoft.Repl.Commanding.DefaultCommandDispatcher`2.ExecuteCommandAsync(IShellState shellState, CancellationToken cancellationToken) in /_/src/Microsoft.Repl/Commanding/DefaultCommandDispatcher.cs:line 117

https://localhost:5001/> dotnet tool -g list
No matching command found
Execute 'help' to see available commands

https://localhost:5001/> dotnet tool install -g Microsoft.dotnet-httprepl
No matching command found
Execute 'help' to see available commands

https://localhost:5001/> exit

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>dotnet tool install -g Microsoft.dotnet-httprepl
Das Tool "microsoft.dotnet-httprepl" ist bereits installiert.

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>httprepl https://localhost:44399/
(Disconnected)> connect https://localhost:44399/
Using a base address of https://localhost:44399/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

https://localhost:44399/> dir
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.

https://localhost:44399/> connect
You must specify either a root address or a base address and an OpenAPI description address

https://localhost:44399/> connect https://localhost:44399
Using a base address of https://localhost:44399/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

https://localhost:44399/> connect https://localhost:44399 --openapi /swagger/v1/swagger.json
Checking https://localhost:44399/swagger/v1/swagger.json... Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte. (localhost:44399)

Using a base address of https://localhost:44399/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

https://localhost:44399/> ls
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.

https://localhost:44399/> connect   C:\Temp
ArgumentCountOutOfRange -- 2
Usage: connect [rootAddress] [--base baseAddress] [--openapi openApiDescriptionAddress] [--verbose]

Configures the directory structure and base address of the api server based on the arguments and options specified. At least one of [rootAddress], [--base baseAddress] or [--openapi openApiDescriptionAddress] must be specified

[rootAddress] will be used to automatically determine the base address and OpenAPI description address
[--base baseAddress] and [--openapi openApiDescriptionAddress] allow you to explicitly set those addresses and skip auto detection
[--verbose] provides more detail about OpenAPI Description discovery and parsing


https://localhost:44399/> connect https://localhost:44399 --c:\Temp
UnknownOption -- --c:\Temp
Usage: connect [rootAddress] [--base baseAddress] [--openapi openApiDescriptionAddress] [--verbose]

Configures the directory structure and base address of the api server based on the arguments and options specified. At least one of [rootAddress], [--base baseAddress] or [--openapi openApiDescriptionAddress] must be specified

[rootAddress] will be used to automatically determine the base address and OpenAPI description address
[--base baseAddress] and [--openapi openApiDescriptionAddress] allow you to explicitly set those addresses and skip auto detection
[--verbose] provides more detail about OpenAPI Description discovery and parsing


https://localhost:44399/> connect https://localhost:44399 --"c:\Temp\"
UnknownOption -- --"c:\Temp"
Usage: connect [rootAddress] [--base baseAddress] [--openapi openApiDescriptionAddress] [--verbose]

Configures the directory structure and base address of the api server based on the arguments and options specified. At least one of [rootAddress], [--base baseAddress] or [--openapi openApiDescriptionAddress] must be specified

[rootAddress] will be used to automatically determine the base address and OpenAPI description address
[--base baseAddress] and [--openapi openApiDescriptionAddress] allow you to explicitly set those addresses and skip auto detection
[--verbose] provides more detail about OpenAPI Description discovery and parsing


https://localhost:44399/> https://localhost:44376/swagger/index.html
No matching command found
Execute 'help' to see available commands

https://localhost:44399/> cd..
No matching command found
Execute 'help' to see available commands

https://localhost:44399/> disconnect
No matching command found
Execute 'help' to see available commands

https://localhost:44399/> exit

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>https://localhost:44376/swagger/index.html
Der Befehl "https:" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>dotnet tool install -g Microsoft.dotnet-httprepl
Das Tool "microsoft.dotnet-httprepl" ist bereits installiert.

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>connect https://localhost:44376/
Der Befehl "connect" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools>httprepl https://localhost:44376/
(Disconnected)> connect https://localhost:44376/
Using a base address of https://localhost:44376/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

https://localhost:44376/> cd
/

https://localhost:44376/> ls
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.

https://localhost:44376/> httprepl https://localhost:44376/ --swagger https://localhost:44376/swagger/v1-foo/swagger.json
No matching command found
Execute 'help' to see available commands

https://localhost:44376/>
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools> httprepl https://localhost:44376/ --swagger https://localhost:44376/swagger/v1-foo/swagger.json
(Disconnected)> connect https://localhost:44376/ --swagger https://localhost:44376/swagger/v1-foo/swagger.json
Checking https://localhost:44376/swagger/v1-foo/swagger.json... Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte. (localhost:44376)

Using a base address of https://localhost:44376/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

https://localhost:44376/> pref set editor.command.default "<path to your editor>"

https://localhost:44376/> dir
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.

https://localhost:44376/>
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Belarmino Vicenzo 26 Reputation points
    2021-05-31T07:22:11.873+00:00

    What is not clear to me, I need to install Swagger?

    No

    What do I want?

    Wait... What?

    I just want to send something and get the answer.
    I want to send something JSON object and return the response.

    First you need to understand how HTTP request work, I can recommend you this ( https://www.youtube.com/watch?v=t5n07Ybz7yI ). It uses a software called Postman.
    Let's say you've done your researches and you know minimally how HTTP request work /are made, headers, params, etc, etc.

    here is a simple http request of type get (as you can see), with no headers, params, just a simple one.
    100947-image.png

    How do to that in C#...Well there's plenty ways to do that.

    First you need a client, a client is who will make the a HTTP Request (I will refer only as request).
    The request is made to using a url and the request return a response(not always data).
    Note: Requests have types (GET,POST,PUT, etc)
    The response is the information that the client asked.
    Assuming you did (or will do) a little research, you understand (or will understand) what I just typed :)

    101023-image.png

    //normal console app  
    
    static void Main(string[] args)  
            {  
    
                var client = new RestClient("https://my-json-server.typicode.com/BelarminoVicenzo/bpgitprojects/posts");  
                client.Timeout = -1;  
                var request = new RestRequest(Method.GET);  
                IRestResponse response = client.Execute(request);  
                Console.WriteLine(response.Content);  
    
                Console.ReadLine();  
    
            }  
    
    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.