or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

content-body-types.mdform-part-customization.mdindex.mdmultipart-entity-building.md

content-body-types.mddocs/

0

# Content Body Types

1

2

Different content body implementations for handling various data types in multipart forms. Content bodies represent the actual content of multipart sections and provide type-specific handling for text, files, binary data, and streams.

3

4

## Capabilities

5

6

### ContentBody Interface

7

8

Base interface for all content body implementations, providing core functionality for multipart content.

9

10

```java { .api }

11

/**

12

* Base interface for multipart content bodies

13

* Extends ContentDescriptor for MIME type information

14

*/

15

public interface ContentBody extends ContentDescriptor {

16

/**

17

* Get the filename for Content-Disposition header

18

* @return Filename string or null if not applicable

19

*/

20

String getFilename();

21

22

/**

23

* Write the content body to an output stream

24

* @param out Output stream to write content to

25

* @throws IOException If an I/O error occurs during writing

26

*/

27

void writeTo(OutputStream out) throws IOException;

28

}

29

```

30

31

### ContentDescriptor Interface

32

33

Provides MIME type and content metadata information for content bodies.

34

35

```java { .api }

36

/**

37

* Interface for content descriptors providing MIME information

38

*/

39

public interface ContentDescriptor {

40

/**

41

* Get the complete MIME type (e.g., "text/plain", "image/jpeg")

42

* @return MIME type string, defaults to "text/plain" if not specified

43

*/

44

String getMimeType();

45

46

/**

47

* Get the MIME media type (e.g., "text", "image", "application")

48

* @return Media type string, defaults to "text" if not specified

49

*/

50

String getMediaType();

51

52

/**

53

* Get the MIME subtype (e.g., "plain", "html", "jpeg")

54

* @return Subtype string, defaults to "plain" if not specified

55

*/

56

String getSubType();

57

58

/**

59

* Get the character encoding for text content

60

* @return Charset string, null for non-text content or when not specified

61

*/

62

String getCharset();

63

64

/**

65

* Get the transfer encoding type

66

* @return Transfer encoding string, defaults to "7bit"

67

*/

68

String getTransferEncoding();

69

70

/**

71

* Get the content length in bytes

72

* @return Content length, or -1 if unknown

73

*/

74

long getContentLength();

75

}

76

```

77

78

### StringBody

79

80

Text content body implementation for string data with character encoding support.

81

82

```java { .api }

83

/**

84

* Text content body backed by a string/byte array

85

* Suitable for form fields, JSON data, XML content, etc.

86

*/

87

public class StringBody extends AbstractContentBody {

88

/**

89

* Create a string body with specified content type

90

* @param text Text content to include

91

* @param contentType Content type (e.g., ContentType.TEXT_PLAIN, ContentType.APPLICATION_JSON)

92

*/

93

public StringBody(String text, ContentType contentType);

94

95

/**

96

* Get the text content length in bytes

97

* @return Content length in bytes

98

*/

99

public long getContentLength();

100

101

/**

102

* String bodies do not have filenames

103

* @return Always returns null

104

*/

105

public String getFilename();

106

107

/**

108

* Write the text content to output stream

109

* @param out Output stream to write to

110

* @throws IOException If writing fails

111

*/

112

public void writeTo(OutputStream out) throws IOException;

113

114

/**

115

* Get content as a Reader for character-based access

116

* @return Reader for the text content

117

* @throws UnsupportedEncodingException If charset is not supported

118

*/

119

public Reader getReader() throws UnsupportedEncodingException;

120

}

121

```

122

123

### FileBody

124

125

Binary content body implementation for file uploads with automatic content type detection and streaming support.

126

127

```java { .api }

128

/**

129

* Binary content body backed by a file

130

* Provides efficient streaming for large files without loading into memory

131

*/

132

public class FileBody extends AbstractContentBody {

133

/**

134

* Create a file body with default binary content type

135

* @param file File to upload

136

*/

137

public FileBody(File file);

138

139

/**

140

* Create a file body with specified content type

141

* @param file File to upload

142

* @param contentType Content type for the file

143

*/

144

public FileBody(File file, ContentType contentType);

145

146

/**

147

* Create a file body with specified content type and custom filename

148

* @param file File to upload

149

* @param contentType Content type for the file

150

* @param filename Custom filename for Content-Disposition header

151

*/

152

public FileBody(File file, ContentType contentType, String filename);

153

154

/**

155

* Get the underlying file object

156

* @return File object being uploaded

157

*/

158

public File getFile();

159

160

/**

161

* Get filename for Content-Disposition header

162

* @return Filename (custom name if specified, otherwise file.getName())

163

*/

164

public String getFilename();

165

166

/**

167

* Get file size as content length

168

* @return File size in bytes

169

*/

170

public long getContentLength();

171

172

/**

173

* Write file content to output stream

174

* @param out Output stream to write file content to

175

* @throws IOException If file reading or writing fails

176

*/

177

public void writeTo(OutputStream out) throws IOException;

178

179

/**

180

* Get input stream for reading file content

181

* @return FileInputStream for the file

182

* @throws FileNotFoundException If file does not exist

183

*/

184

public InputStream getInputStream() throws FileNotFoundException;

185

}

186

```

187

188

### ByteArrayBody

189

190

Binary content body implementation for in-memory byte arrays with efficient direct access.

191

192

```java { .api }

193

/**

194

* Binary content body backed by a byte array

195

* Suitable for small to medium binary data already in memory

196

*/

197

public class ByteArrayBody extends AbstractContentBody {

198

/**

199

* Create byte array body with default binary content type

200

* @param data Byte array containing the binary data

201

* @param filename Filename for Content-Disposition header

202

*/

203

public ByteArrayBody(byte[] data, String filename);

204

205

/**

206

* Create byte array body with specified content type and filename

207

* @param data Byte array containing the binary data

208

* @param contentType Content type for the binary data

209

* @param filename Filename for Content-Disposition header

210

*/

211

public ByteArrayBody(byte[] data, ContentType contentType, String filename);

212

213

/**

214

* Get filename for Content-Disposition header

215

* @return Filename specified in constructor

216

*/

217

public String getFilename();

218

219

/**

220

* Get byte array length as content length

221

* @return Length of the byte array

222

*/

223

public long getContentLength();

224

225

/**

226

* Write byte array content to output stream

227

* @param out Output stream to write array content to

228

* @throws IOException If writing fails

229

*/

230

public void writeTo(OutputStream out) throws IOException;

231

}

232

```

233

234

### InputStreamBody

235

236

Binary content body implementation for streaming data from input streams with unknown content length.

237

238

```java { .api }

239

/**

240

* Binary content body backed by an input stream

241

* Suitable for large data streams or when data source is not known in advance

242

* Content length is typically unknown (-1)

243

*/

244

public class InputStreamBody extends AbstractContentBody {

245

/**

246

* Create input stream body with specified content type

247

* @param in Input stream containing the binary data

248

* @param contentType Content type for the stream data

249

*/

250

public InputStreamBody(InputStream in, ContentType contentType);

251

252

/**

253

* Create input stream body with specified content type and filename

254

* @param in Input stream containing the binary data

255

* @param contentType Content type for the stream data

256

* @param filename Filename for Content-Disposition header

257

*/

258

public InputStreamBody(InputStream in, ContentType contentType, String filename);

259

260

/**

261

* Create input stream body with default binary content type

262

* @param in Input stream containing the binary data

263

* @param filename Filename for Content-Disposition header

264

*/

265

public InputStreamBody(InputStream in, String filename);

266

267

/**

268

* Get the underlying input stream

269

* @return Input stream containing the data

270

*/

271

public InputStream getInputStream();

272

273

/**

274

* Get filename for Content-Disposition header

275

* @return Filename specified in constructor, or null

276

*/

277

public String getFilename();

278

279

/**

280

* Stream content length is typically unknown

281

* @return Always returns -1 (unknown length)

282

*/

283

public long getContentLength();

284

285

/**

286

* Write stream content to output stream

287

* @param out Output stream to write stream content to

288

* @throws IOException If reading from input stream or writing fails

289

*/

290

public void writeTo(OutputStream out) throws IOException;

291

}

292

```

293

294

### AbstractContentBody

295

296

Base implementation class providing common functionality for content body implementations.

297

298

```java { .api }

299

/**

300

* Abstract base class for content body implementations

301

* Provides ContentDescriptor implementation based on ContentType

302

*/

303

public abstract class AbstractContentBody implements ContentBody {

304

/**

305

* Create abstract content body with specified content type

306

* @param contentType Content type information

307

*/

308

public AbstractContentBody(ContentType contentType);

309

310

/**

311

* Get the ContentType object

312

* @return ContentType instance with full type information

313

*/

314

public ContentType getContentType();

315

316

// ContentDescriptor implementations

317

public String getMimeType();

318

public String getMediaType();

319

public String getSubType();

320

public String getCharset();

321

322

// Abstract methods - must be implemented by subclasses

323

public abstract String getTransferEncoding();

324

public abstract long getContentLength();

325

public abstract void writeTo(OutputStream out) throws IOException;

326

public abstract String getFilename();

327

}

328

```

329

330

**Usage Examples:**

331

332

```java

333

import org.apache.http.entity.mime.content.*;

334

import org.apache.http.entity.ContentType;

335

import java.io.File;

336

import java.io.FileInputStream;

337

import java.nio.charset.StandardCharsets;

338

339

// Text content for form fields

340

StringBody username = new StringBody("john_doe", ContentType.TEXT_PLAIN);

341

StringBody jsonData = new StringBody("{\"type\":\"user\",\"active\":true}",

342

ContentType.APPLICATION_JSON);

343

344

// File uploads

345

FileBody document = new FileBody(new File("report.pdf"), ContentType.APPLICATION_PDF);

346

FileBody image = new FileBody(new File("photo.jpg"), ContentType.IMAGE_JPEG, "profile-photo.jpg");

347

348

// Binary data from arrays

349

byte[] imageBytes = // ... load image data

350

ByteArrayBody avatar = new ByteArrayBody(imageBytes, ContentType.IMAGE_PNG, "avatar.png");

351

352

// Streaming data

353

FileInputStream stream = new FileInputStream("large-file.zip");

354

InputStreamBody largeFile = new InputStreamBody(stream, ContentType.APPLICATION_OCTET_STREAM, "archive.zip");

355

356

// Custom content with specific charset

357

ContentType customType = ContentType.create("text/csv", StandardCharsets.UTF_8);

358

StringBody csvData = new StringBody("name,email\nJohn,john@example.com", customType);

359

```