MCPcopy Create free account
hub / github.com/langgenius/dify / buildBody

Function buildBody

cli/src/http/body.ts:42–63  ·  view source on GitHub ↗
(input: BuildBodyInput)

Source from the content-addressed store, hash-verified

40// json wins over body when both are provided; tests assert single-source-of-truth via type system,
41// but at runtime we still prefer json explicitly.
42export function buildBody(input: BuildBodyInput): BuildBodyResult {
43 const { json, body, method } = input
44 const isPayloadMethod = method !== 'GET' && method !== 'HEAD'
45
46 if (json !== undefined) {
47 if (!isPayloadMethod)
48 return { body: undefined, contentType: undefined }
49 if (isJSONSerializable(json))
50 return { body: JSON.stringify(json), contentType: 'application/json' }
51 return { body: json as BodyInit, contentType: undefined }
52 }
53
54 // A raw `body` is passed through untouched. This is replay-safe only for buffered
55 // payloads (string, Blob, FormData, typed arrays) — a single-shot ReadableStream
56 // would be consumed on the first attempt and replay empty on retry. Safe today
57 // because the only stream/multipart caller (file-upload) uses POST, which is not
58 // in RETRY_METHODS; revisit if a ReadableStream body is ever sent over GET/PUT/DELETE.
59 if (body !== undefined && isPayloadMethod)
60 return { body, contentType: undefined }
61
62 return { body: undefined, contentType: undefined }
63}

Callers 2

buildRequestFunction · 0.85
body.test.tsFile · 0.85

Calls 1

isJSONSerializableFunction · 0.85

Tested by

no test coverage detected