Giter Club home page Giter Club logo

http2curl's Introduction

http2curl

๐Ÿ“ Convert Golang's http.Request to CURL command line

go.dev reference License GitHub release Docker Metrics Made by Manfred Touron

Go Release PR GolangCI codecov Go Report Card CodeFactor

To do the reverse operation, check out mholt/curl-to-go.

Example

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

Install

go get moul.io/http2curl

Usages

License

ยฉ 2019-2021 Manfred Touron

Licensed under the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option. See the COPYRIGHT file for more details.

SPDX-License-Identifier: (Apache-2.0 OR MIT)

http2curl's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

http2curl's Issues

invalid generated curl command with url.Values [BUG]

Describe the bug

when the request body is encoded using (url.Values{}).Encode() , the -d flag should not be used. Instead, the encoded string should be appended directly to the URL

To Reproduce
Steps to reproduce the behavior:

req, _ := http.NewRequest("PUT", "http://www.example.com/abc/def.ghi?jlk=mno&pqr=stu", bytes.NewBufferString((url.Values{"test": []string{"1", "2"}}).Encode()))
command, _ := http2curl.GetCurlCommand(req)
fmt.Println(command)

// output: curl -X 'PUT' -d 'test=1&test=2' 'http://www.example.com'  

Expected behavior
the output should be curl -X 'PUT' 'http://www.example.comtest=1&test=2'

Versions (please complete the following information, if relevant):

  • Software version: v1.0.0
  • OS: Mac
  • Golang version: 1.21.4

Doesn't seem to always quote body data

Here's a failing test

func TestItQuotesFormData(t *testing.T){
    form := url.Values{}
    form.Add("age", "10")
    form.Add("name", "Hudson")
    body := form.Encode()

    req, _ := http.NewRequest(http.MethodPost, "http://foo.com/cats", ioutil.NopCloser(bytes.NewBufferString(body)))
    req.Header.Set("API_KEY", "123")

    command, _ := GetCurlCommand(req)

    expected := `curl -X POST -d "age=10&name=Hudson" -H "API_KEY: 123" 'http://foo.com/cats'`

    if command.String() != expected{
        t.Error("Didnt work, got")
        t.Log(command.String())
        t.Log("expected")
        t.Log(expected)
    }

}

Gets

    http2curl_test.go:56: Didnt work, got
    http2curl_test.go:57: curl -X POST -d age=10&name=Hudson -H "API_KEY: 123" 'http://foo.com/cats'
    http2curl_test.go:58: expected
    http2curl_test.go:59: curl -X POST -d "age=10&name=Hudson" -H "API_KEY: 123" 'http://foo.com/cats'

The resulting curl is not valid because the form data needs to be quoted. I tried playing with your algorithm for quoting but ended up breaking lots of stuff. Will give it a go later unless you can see an easy fix?

Package gorequest broken

Since today, I'm facing the below issue with the package "gorequest" about this package.

Did something change? Can I do something in my app to fix it?

../github.com/parnurzeal/gorequest/gorequest.go:31:2: cannot find package "github.com/moul/http2curl" in any of:
	/usr/local/go/src/github.com/moul/http2curl (from $GOROOT)
	/go/src/github.com/moul/http2curl (from $GOPATH)

Thanks in advance,

[BUG] http: ContentLength=xx with Body length 0

Describe the bug
A clear and concise description of what the bug is.

after #31 merged

client.Do(req) https request will return http: ContentLength=xx with Body length 0 error after http2curl.GetCurlCommand(req) called

To Reproduce

str := `{"hello":"world"}`
req, _ := http.NewRequest("POST", `https://www.baidu.com`, strings.NewReader(str))
req.Header.Set("Content-Length", strconv.FormatInt(int64(len(str)), 10))
client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    },
}
http2curl.GetCurlCommand(req)
_, err := client.Do(req)
if err != nil {
    fmt.Println("err: ", err)
}

/cc @Dzalevski

Difference to net/http package

Hi. I'd like to know what is the differences between use the curl command to use the native net/http package to perform http requests. I mean, there's differences in DNS resolution, timeouts, or any configuration that could work different to the same request?

I'm asking this because before I was using a 3rd party package that use this http2curl package to perform the requests, and now, I'm using another one that uses the net/http package.
Unfortunately, for the same request, I'm seeing o lot of timeouts which dit not happen before.

Thanks in advance.

[BUG] GetCurlCommand() can't get the request host

Describe the bug

GetCurlCommand() can not get the request host.

To Reproduce

package main

import (
	"fmt"
	"net/http"

	"moul.io/http2curl"
)

func greet(w http.ResponseWriter, r *http.Request) {
	command, _ := http2curl.GetCurlCommand(r)
	fmt.Fprintf(w, command.String())
}

func main() {
	http.HandleFunc("/", greet)
	http.ListenAndServe(":8080", nil)
}

$ curl --location --request GET 'http://127.0.0.1:8080/'

Output: curl -X 'GET' -d '' -H 'Accept: */*' -H 'User-Agent: curl/7.77.0' '/'

Expect output: curl -X 'GET' -d '' -H 'Accept: */*' -H 'User-Agent: curl/7.77.0' 'http://127.0.0.1:8080/'

Remove goconvey?

There seems to only be one test using it and these can just be written as plain Go tests, or use the more common testify.

Allow access to slice

It would be nicer to use:

type CurlCommand []string

So that I can print it as a multiline:

fmt.Println(strings.Join(command, " \\\n"))

[BUG] not possible to "install" module

Describe the bug
Not possible to "install/get" module

To Reproduce
Steps to reproduce the behavior:

  1. 'go get moul.io/http2curl'
  2. See error:
    'go: moul.io/http2curl upgrade => v2.0.0+incompatible
    go get: moul.io/[email protected]+incompatible/go.mod: verifying module: moul.io/[email protected]+incompatible/go.mod: reading https://gocenter.io/sumdb/sum.golang.org/lookup/moul.io/[email protected]+incompatible: 404 Not Found'

Expected behavior
being able to install module and use it in go

Screenshots / Logs
'go: moul.io/http2curl upgrade => v2.0.0+incompatible
go get: moul.io/[email protected]+incompatible/go.mod: verifying module: moul.io/[email protected]+incompatible/go.mod: reading https://gocenter.io/sumdb/sum.golang.org/lookup/moul.io/[email protected]+incompatible: 404 Not Found'

Versions (please complete the following information, if relevant):

  • Software version: 'v2.0.0+incompatible'
  • OS: OSX:10:15:6
  • Golang version: go version go1.14.5 darwin/amd64
  • GOPROXY=https://gocenter.io (but same error as well witout proxy)

Additional context
Add any other context about the problem here.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/go.yml
  • actions/checkout v3
  • golangci/golangci-lint-action v3.3.0
  • actions/checkout v3
  • actions/setup-go v3
  • actions/checkout v3
  • actions/setup-go v3
  • actions/cache v3.0.7
  • codecov/codecov-action v3.1.0
  • actions/checkout v3
  • actions/setup-go v3
  • actions/cache v3.0.7
  • codecov/codecov-action v3.1.0
.github/workflows/pr.yml
  • actions/checkout v3
  • snyk/release-notes-preview v1.6.2
  • actions/checkout v3
.github/workflows/release.yml
  • actions/checkout v3
  • ghcr.io/codfish/semantic-release-action v1
  • actions/setup-go v3
gomod
go.mod
  • go 1.13
  • github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502@720c4b409502
npm
tool/lint/package.json
  • alex 10.0.0
  • markdown-spellcheck 1.3.1
  • markdownlint-cli 0.31.1
  • remark-cli 10.0.1
  • remark-lint 9.1.1

  • Check this box to trigger a request for Renovate to run again on this repository

[BUG] expects import

Describe the "bug"
It's a little replacement that could solve an issue into doc.go file, if you don't mind take a look into it to be noticed that you should remove "// import "moul.io/http2curl/v2" from github.com/moul/http2curl/doc.go 'cause it's reproducing the error below

../../gopkg.in/ns3777k/go-shodan.v3/shodan/shodan.go:7:2: code in directory /Users/richardsonlima/go-workspace/src/github.com/moul/http2curl expects import "moul.io/http2curl"

Versions:

  • OS: [Mac 11.0.1 (20B29)]
  • Golang version [go1.15.4 darwin/amd64]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.