MCPcopy Index your code
hub / github.com/GitbookIO/gitbook / getCurlBody

Function getCurlBody

packages/react-openapi/src/code-samples.ts:230–272  ·  view source on GitHub ↗
(body: any, headers?: Record<string, string>)

Source from the content-addressed store, hash-verified

228// Body Generators
229const BodyGenerators = {
230 getCurlBody(body: any, headers?: Record<string, string>) {
231 if (!body || !headers) return undefined;
232
233 // Copy headers to avoid mutating the original object
234 const headersCopy = { ...headers };
235 const contentType: string = headersCopy['Content-Type'] || '';
236
237 if (isFormData(contentType)) {
238 body = isPlainObject(body)
239 ? Object.entries(body).map(([key, value]) => `--form '${key}=${String(value)}'`)
240 : `--form 'file=@${body}'`;
241 } else if (isFormUrlEncoded(contentType)) {
242 body = isPlainObject(body)
243 ? `--data '${Object.entries(body)
244 .map(([key, value]) => `${key}=${String(value)}`)
245 .join('&')}'`
246 : String(body);
247 } else if (isText(contentType)) {
248 body = `--data '${String(body).replace(/"/g, '')}'`;
249 } else if (isXML(contentType)) {
250 // Convert to XML and ensure proper formatting
251 body = `--data-binary $'${convertBodyToXML(body)}'`;
252 } else if (isCSV(contentType)) {
253 // We use --data-binary to avoid cURL converting newlines to \r\n
254 body = `--data-binary $'${stringifyOpenAPI(body).replace(/"/g, '').replace(/\\n/g, '\n')}'`;
255 } else if (isGraphQL(contentType)) {
256 body = `--data '${stringifyOpenAPI(body)}'`;
257 // Set Content-Type to application/json for GraphQL, recommended by GraphQL spec
258 headersCopy['Content-Type'] = 'application/json';
259 } else if (isPDF(contentType)) {
260 // We use --data-binary to avoid cURL converting newlines to \r\n
261 body = `--data-binary '@${String(body)}'`;
262 } else if (isYAML(contentType)) {
263 body = `--data-binary $'${yaml.dump(body).replace(/'/g, '').replace(/\\n/g, '\n')}'`;
264 } else {
265 body = `--data '${stringifyOpenAPI(body, null, 2).replace(/\\n/g, '\n')}'`;
266 }
267
268 return {
269 body,
270 headers: headersCopy,
271 };
272 },
273 getJavaScriptBody: (body: any, headers?: Record<string, string>) => {
274 if (!body || !headers) return;
275

Callers

nothing calls this directly

Calls 11

isFormDataFunction · 0.90
isPlainObjectFunction · 0.90
isFormUrlEncodedFunction · 0.90
isTextFunction · 0.90
isXMLFunction · 0.90
isCSVFunction · 0.90
stringifyOpenAPIFunction · 0.90
isGraphQLFunction · 0.90
isPDFFunction · 0.90
isYAMLFunction · 0.90
convertBodyToXMLFunction · 0.85

Tested by

no test coverage detected