Skip to content

Commit c927fc5

Browse files
authored
Added support for urlencoded form content type (#43)
1 parent 0aa568d commit c927fc5

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ <h1>curl-to-Go</h1>
2828
<h2>Instantly convert <a href="http://curl.haxx.se/">curl</a> commands to <a href="https://golang.org/">Go</a> code</h2>
2929

3030
<p>
31-
This tool turns a curl command into Go code. (To do the reverse, check out <a href="https://github.com/sethgrid/gencurl">sethgrid/gencurl</a>.) Currently, it knows the following options: -d/--data, -H/--header, -I/--head, -u/--user, --url, and -X/--request. It also understands JSON content types (see <a href="https://mholt.github.io/json-to-go">JSON-to-Go</a>). Feel free to <a href="https://github.com/mholt/curl-to-go">contribute on GitHub</a>!
31+
This tool turns a curl command into Go code. (To do the reverse, check out <a href="https://github.com/sethgrid/gencurl">sethgrid/gencurl</a>.) Currently, it knows the following options: -d/--data, -H/--header, -I/--head, -u/--user, --url, and -X/--request. It also understands JSON content types (see <a href="https://mholt.github.io/json-to-go">JSON-to-Go</a>). If the content type is application/x-www-form-urlencoded then it will convert the date to <a href="https://pkg.go.dev/net/url#Values">Values</a>(same as <a href="https://pkg.go.dev/net/http#Client.PostForm">PostForm</a>). Feel free to <a href="https://github.com/mholt/curl-to-go">contribute on GitHub</a>!
3232
</p>
3333

3434
<p class="examples">
3535
<a href="javascript:" id="example1">Example 1</a> &middot;
3636
<a href="javascript:" id="example2">Example 2</a> &middot;
3737
<a href="javascript:" id="example3">Example 3</a> &middot;
3838
<a href="javascript:" id="example4">Example 4</a> &middot;
39-
<a href="javascript:" id="example5">Example 5</a>
39+
<a href="javascript:" id="example5">Example 5</a> &middot;
40+
<a href="javescript:" id="example6">Example 6</a>
4041
</p>
4142
</header>
4243

resources/js/common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ $(function()
8585
$('#example5').click(function() {
8686
$('#input').val("curl -X POST https://api.easypost.com/v2/shipments \\\n -u API_KEY: \\\n -d 'shipment[to_address][id]=adr_HrBKVA85' \\\n -d 'shipment[from_address][id]=adr_VtuTOj7o' \\\n -d 'shipment[parcel][id]=prcl_WDv2VzHp' \\\n -d 'shipment[is_return]=true' \\\n -d 'shipment[customs_info][id]=cstinfo_bl5sE20Y'").keyup();
8787
});
88+
$('#example6').click(function(){
89+
$('#input').val('curl https://api.example.com/surprise -X POST -H "Content-Type: application/x-www-form-urlencoded" \\\n --data "key1=value+1&key2=value%3A2"').keyup();
90+
});
8891

8992
var dark = false;
9093
$("#dark").click(function()

resources/js/curl-to-go.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ function curlToGo(curl) {
113113
go += 'payloadBytes, err := json.Marshal(data)\n'+err;
114114
go += defaultPayloadVar+' := bytes.NewReader(payloadBytes)\n\n';
115115
}
116-
} else {
116+
} else if(req.headers["Content-Type"] && req.headers["Content-Type"] == "application/x-www-form-urlencoded") {
117+
go += "params := url.Values{}\n"
118+
var params = new URLSearchParams(req.data.ascii);
119+
params.forEach(function(fvalue, fkey){
120+
go += 'params.Add("' + fkey + '", `' + fvalue + '`)\n'
121+
});
122+
go += defaultPayloadVar+ ' := strings.NewReader(params.Encode())\n\n'
123+
}else {
117124
// not a json Content-Type, so treat as string
118125
stringBody();
119126
}

0 commit comments

Comments
 (0)