Issue
In my debugging process of a microservice, I want to know how my client service request finally looks like, to make sure is the problem from client service or the target service.
For this purpose, I need a tool (or pkg) to turn http.Request
struct to a cURL command
example of http.Request struct:
{
Method:POST
URL:https://some-fake-url.com
Proto:HTTP/1.1
ProtoMajor:1
ProtoMinor:1
Accept-Encoding:[gzip, deflate, br]
Accept-Language:[en-US,en;q=0.9]
Authorization:[Bearer gkasgjagsljkg]
Connection:[keep-alive]
Content-Length:[0]
Content-Type:[application/json]
...
}
The client service use this request with http.DefaultClient.Do()
,
I don't want to manually turn this into a cURL because that may not be valid.
Solution
You can turn your Golang requests to cURL command with http2curl pkg like below:
import (
"http"
"moul.io/http2curl"
)
data := bytes.NewBufferString(`{"hello":"world","answer":42}`)
req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", data)
req.Header.Set("Content-Type", "application/json")
command, _ := http2curl.GetCurlCommand(req)
fmt.Println(command)
// Output: curl -X PUT -d "{\"hello\":\"world\",\"answer\":42}" -H "Content-Type: application/json" http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu
Answered By - Arsham Arya Answer Checked By - David Goodson (WPSolving Volunteer)