27 Feb ameshkov gocurl: Simplified version of curl written in pure Go with additional features
Let’s send a request to cloudflare.comand use crypto.cloudflare.com as a client-facing server for that. A command-line tool to make GET requests to any URL using Go and Cobra. Additional changes can be made to handle different HTTP methods, headers, and more. We will send this request to the server using net.Dial and read the response.
It does not guarantee high-fidelity conversions, but it’s good enough for most API docs that have curl samples. Most of us do not specify the port when making a day-to-day HTTP request using curl or a browser. To make our UX better, let’s set the default port to 80 if it is not specified in the URL. In this tutorial, I will not handle HTTPS requests, which is why we are going to use only port HTTP (80) for now. To achieve this, we need to parse the URL provided as an argument, extract the hostname, port, and path, and then make an HTTP GET request to the specified URL.
Search code, repositories, users, issues, pull requests…
We will then print the response to the console. This said, libcurl API can be less easy to learn than net/http. Interesting thing about ECH is that it may connect even if you use an expiredconfiguration (see HelloRetryRequest in the RFC). It depends on both theserver and the client implementation and does not work with Cloudflare at themoment. We read every piece of feedback, and take your input very seriously. In this case, gocurl will try to discover the ECH configuration from DNSrecords and then use them to establish the connection.
Experimental flags
Some comments may only be visible to logged-in visitors. Following comes from examples/https.goSimply type go run ./examples/https.go to execute. This package is not in the latest version of its module. For this command to work you may need to replace –echconfig with thecurrent one discovered using DNS as was explained before.
type CURL ¶
Now that we have our project set up, let’s define our CLI commands using Cobra. We’ll create a simple command to make HTTP GET requests. It is a challenge because handling TCP connections and HTTP requests can be complex, but we’ll keep it simple and focus on the basics.
type VersionInfoData ¶
I actually made a golang version of curl for myself, because Microsoft got rid of it in their .Net 5 docker images, which ended up breaking my images when I upgraded. You’ve built a simple command-line tool similar to curl using Go and Cobra. Feel free to expand upon this project by adding more features like handling different HTTP methods, headers, and more. Curl has like a bajillion options, so don’t expect all of them to be implemented; just the most common/important ones to stub out code from API samples and docs, etc.
func (*CURL) Send ¶
But feel free to open an issue or submit a pull request! To request a new flag, please provide the equivalent Go code. In the code above, we create a TCP connection to the specified host and port. We then send an HTTP GET request to the server and read the response. If you run the application with a valid URL, you should see the HTTP response printed to the console.
- This said, libcurl API can be less easy to learn than net/http.
- But if you run the application with an URL as an argument, you should see that hostname, port, and path are printed to the console.
- You may want to add custom validators or other Cobra built-in validators if you want to expand this functionality.
- It does not guarantee high-fidelity conversions, but it’s good enough for most API docs that have curl samples.
It is also possible to create subcommands in separate files within the cmd directory. For now, we’ll keep it simple and define everything in the root.go file. This is a sister tool to JSON-to-Go, which converts JSON to a Go struct type definition.
- To request a new flag, please provide the equivalent Go code.
- Most of us do not specify the port when making a day-to-day HTTP request using curl or a browser.
- It seemed easier tome to implement the necessary parts of curl in Go.
- It uses http package instead of net which shows a more simple and fast way to implement a cURL client in Go without making requests from scratch using net.Dial.
- Run the application to see the default port bring printed to the console.
It uses http package instead of net which shows a more simple and fast way to implement a cURL client in Go without making requests from scratch using net.Dial. The Args field specifies the number of arguments the command expects. In this case, we expect exactly one argument, which is the URL we want to make a request to. You may want to add custom validators or other Cobra built-in validators if you golang curl want to expand this functionality. If you run the application without an argument, you should see an error message printed to the console. The cmd directory contains the root command file, which is where we’ll define our whole application.
Also, you can use some new stuff that is not supported by curl. Just give the code a once-over since it will need you to flesh out some parts like error handling. Now that we have the necessary information to create a TCP connection, let’s make an HTTP GET request to the specified URL. Run the application to see the default port bring printed to the console. As you probably have noticed, the port is not being extracted correctly. This is because the url.Parse function does not return the port if it is not specified in the URL.
We developers sometimes are so focused on using libraries and abstractions that the basic stuff stays a bit aside. So the main purpose of using net.Dial is to create indeed a HTTP request from scratch and remember a bit of the basics like using TCP protocol. Indeed cURL has a lot of features which would be a pain to replicate in a personal project. The choice of using net.Dial instead of http is to show a bit of how things work under the hood of a HTTP client package. You may want to configure a specific “client-facing” server instead and the wayto do that is to use –connect-to.
Firstly, we should parse the URL and extract the necessary information to create our TCP connection. Any curl command should work, but only certain flags are understood and converted into Go code. I have made a project called gurl that is a simple CLI tool that can make HTTP requests using Go.
Sorry, the comment form is closed at this time.