Trexmi
Converter Ready

cURL to Go net/http

Convert one cURL HTTP request into complete Go net/http code with method, headers, inline body, redirect policy, client timeout, response close, and status checks.

Generate a complete package main example Create requests with http.NewRequest Preserve common headers and inline body Map -L into client redirect behavior
No request is sent. The converter supports one HTTP(S) URL plus common method, header, inline body, auth, cookie, redirect, HEAD, GET-query, and timeout options. Files, multipart forms, proxies, certificates, and unsupported flags are rejected.
INPUT cURL command *
0 chars0 words0 lines
Ctrl / ⌘ + Enter
Generated code
About the tool

What cURL to Go net/http does

cURL to Go HTTP Converter creates a complete Go program using the standard net/http package. The output builds an http.Request, applies parsed headers, configures an http.Client, performs the request, closes the response body, reads it, and checks the HTTP status.

The input is parsed as text and is never executed. One HTTP or HTTPS URL is accepted. Common inline data, header, authentication, cookie, redirect, GET-query, HEAD, and timeout options are supported; ambiguous features such as file uploads, multipart forms, proxies, certificates, and multiple transfers are rejected.

The generated program is intentionally direct. A production Go service will often add a context, dependency-injected client, structured errors, a limited response reader, tracing, retry policy, and typed JSON decoding. Those decisions depend on the application and cannot be inferred from a cURL snippet.

cURL to Go HTTP converter generating a net/http request client timeout and response checks
Convert the visible request while keeping Go client and response lifecycle decisions explicit.

How to use

  1. Paste one command. Begin with curl and provide one absolute HTTP(S) URL.
  2. Inline the payload. File-backed @payload, multipart forms, and uploads are deliberately refused.
  3. Generate Go code. The body becomes a strings.Reader; each parsed header is added to the request.
  4. Inspect client policy. Confirm timeout, redirect behavior, status handling, authentication, and expected response type.
  5. Adapt for production. Run gofmt, pass a context, limit response size, decode the expected media type, and test safely.
Built for the task

Why use cURL to Go net/http?

Focused controls, predictable output, and a workflow designed around this exact transformation.

01

Runnable Go structure

Generate imports, main function, request construction, client execution, and output handling.

02

Client timeout included

Use --max-time when supplied or a 30-second default instead of http.DefaultClient without a total timeout.

03

Response lifecycle visible

Close resp.Body and check errors from both the request and body read.

04

Redirect intent preserved

Disable automatic redirects unless the source cURL command includes -L.

Useful answers

Questions about cURL to Go net/http

Practical details about input, output, privacy, limits, and the best way to use this tool.

01 Does cURL to Go HTTP execute the request?

No. It generates Go source only. Nothing is compiled and the URL is not contacted by the converter.

02 Why does the code use http.NewRequest?

It provides one request object where the custom method, body, and headers can be reviewed before Client.Do.

03 Why not use http.DefaultClient?

The generated code needs an explicit total timeout and redirect policy. A dedicated client makes those choices visible.

04 Is resp.Body always closed?

The generated code defers resp.Body.Close() immediately after a successful request, as required for responsible response handling.

05 Should production code use context.Context?

Usually yes. Build the request with http.NewRequestWithContext when cancellation, deadlines, tracing, or request-scoped work must propagate.

06 Does a non-2xx status make Client.Do return an error?

No. The generated program checks StatusCode separately and reports the response body with the HTTP status.

07 Can the converter generate multipart uploads?

No. A correct Go multipart request needs a multipart writer, field and file handling, content type boundary, and resource cleanup.

08 What else should I add before production?

Consider response-size limits, typed decoding, context cancellation, observability, safe retries, redacted logs, a shared client, and application-specific error types.

Learn HTTP & Network Debugging

Read the HTTP Headers Guide

Connect DNS, redirects, HTTP headers, requests, responses, caching, security, and practical web diagnostics.

  • Requests and response headers
  • Redirect and cache diagnostics
  • Security and API debugging
Read guide Practical explanations and examples

Examples

Generate a JSON POST client

The inline JSON remains a string body and the media type remains an explicit header.

Input
curl 'https://api.example.com/v1/events' -H 'Content-Type: application/json' -d '{"type":"build.completed"}'
Output
http.NewRequest("POST", url, strings.NewReader("{...}")) · req.Header.Set(...) · client.Do(req)

Generate a HEAD request

HEAD is represented without a request body and the timeout is bounded.

Input
curl -I --max-time 5 'https://example.com/health'
Output
http.NewRequest("HEAD", ...) · http.Client{Timeout: 5000 * time.Millisecond, ...}

Keep redirects disabled by default

Adding -L removes that callback so the Go client can use its normal redirect handling.

Input
curl 'https://example.com/old-path'
Output
CheckRedirect returns http.ErrUseLastResponse

Reject a client certificate command

Go TLS client certificates require loading credentials and building a dedicated Transport.

Input
curl --cert client.pem --key client.key https://api.example.com/private
Output
Error: certificate options are not supported because transport configuration must be implemented manually.

From cURL tokens to an http.Request

The parsed method and URL become arguments to http.NewRequest. An inline body becomes a strings.Reader, while a request without data uses nil. Parsed headers are applied with Header.Set. With -G, data is appended to the URL query and no body reader is created.

Check complex endpoints with URL Parser and inspect encoded or repeated parameters with Query String Parser.

Timeouts and redirect policy

The generated http.Client has a total timeout expressed in milliseconds. It also returns http.ErrUseLastResponse from CheckRedirect when the cURL source omitted -L. This keeps a redirect response available instead of silently following it.

A production service may need separate dial, TLS handshake, response header, and idle connection timeouts in an http.Transport. The single client timeout is a safe, readable starting point rather than a complete transport policy.

Body ownership, status, and decoding

When Client.Do succeeds, the caller owns a non-nil response body and must close it. The output checks the read error and then treats statuses outside 200–299 as failures. Adjust that range if redirects, 304, or another response is valid for your application.

For public endpoint diagnostics, use HTTP Header Checker. To keep the same request in PHP, use cURL to PHP cURL.

Unsupported cURL features

The parser supports one HTTP(S) request and common options only. It does not evaluate shell variables, substitutions, or local paths. Multipart forms, file uploads, proxy settings, certificate configuration, insecure TLS, cookie jars, multiple URLs, and unknown flags are rejected so missing behavior is not hidden.

Confirm cURL flags in the official cURL manual and request, client, redirect, and body responsibilities in the Go net/http package documentation.