Send JSON with cURL in Java
Paste a curl command that sends JSON and get idiomatic Java code. We pick the right serialization for OkHttp and set the correct Content-Type.
Quick answer
To convert a cURL command to Java, paste it into the Toolpaper workbench below. It parses the command in your browser and emits idiomatic Java code — headers, query string, request body, authentication (Bearer, Basic, API key), and cookies are all wired up automatically. No data is uploaded; conversion is fully client-side.
Prefer a different target? See the full cURL to Java reference, or jump to the same guide in JavaScript, Node.js, or Python.
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Main {
public static void main(String[] args) throws Exception {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
clientBuilder.followRedirects(false).followSslRedirects(false);
OkHttpClient client = clientBuilder.build();
RequestBody body = RequestBody.create("{\"name\":\"Ada\",\"email\":\"ada@example.com\"}", MediaType.parse("application/json"));
Request.Builder rb = new Request.Builder().url("https://api.example.com/users");
rb.method("POST", body);
rb.header("Content-Type", "application/json");
try (Response response = client.newCall(rb.build()).execute()) {
System.out.println(response.body().string());
}
}
}Example
A representative curl command for this scenario. Paste your own above to convert an exact match.
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Ada","email":"ada@example.com"}'Implementation notes
- Content-Type is detected from -H headers first, then inferred from the body shape when the header is missing.
- JSON bodies are parsed and re-emitted through OkHttp's serializer so quoting stays consistent with what the server expects.
- Charset directives on Content-Type (e.g. application/json; charset=utf-8) survive the round trip.
Common mistakes
- Setting Content-Type manually and then also using OkHttp's helper — you end up with duplicate headers or the wrong one wins.
- Mixing form encoding with JSON headers — the server rejects with 400 and it's easy to miss in code review.
- Trusting the browser DevTools "Copy as cURL" to preserve charset — it often drops it. Clean panel restores common defaults.
Common questions
- How is JSON encoded in the generated Java?
- Toolpaper detects Content-Type: application/json and emits idiomatic OkHttp JSON serialization (json= for requests, Response.json for Go, etc.).
- Does it preserve Content-Type?
- Yes. The Content-Type header is honored and matched against the body so serialization stays consistent.
- Is anything uploaded?
- No. Parsing and generation happen entirely in your browser.
More payload types in Java
Java hub →The same guide in other languages
All guides →In JavaScript
/how-to/send-json-with-curl-in-javascript
In Node.js
/how-to/send-json-with-curl-in-node
In Python
/how-to/send-json-with-curl-in-python
In Go
/how-to/send-json-with-curl-in-go
In PHP
/how-to/send-json-with-curl-in-php
In Ruby
/how-to/send-json-with-curl-in-ruby
In Rust
/how-to/send-json-with-curl-in-rust
In Kotlin
/how-to/send-json-with-curl-in-kotlin
Full Java reference on the cURL to Java hub, or open the main converter for all 18 targets.