MCPcopy Create free account
hub / github.com/scalar/scalar / buildRequestBody

Function buildRequestBody

packages/snippetz/src/plugins/python/python3/python3.ts:54–170  ·  view source on GitHub ↗
(
  request: ReturnType<typeof normalizeRequest>,
  headers: Record<string, string>,
  imports: Set<string>,
)

Source from the content-addressed store, hash-verified

52}
53
54const buildRequestBody = (
55 request: ReturnType<typeof normalizeRequest>,
56 headers: Record<string, string>,
57 imports: Set<string>,
58): BodyGenerationResult => {
59 const postData = request.postData
60
61 if (!postData) {
62 return {
63 payloadLines: [],
64 }
65 }
66
67 const { mimeType, text, params } = postData
68
69 if (mimeType === 'application/json' && text) {
70 try {
71 const parsed = JSON.parse(text)
72 imports.add('import json')
73
74 return {
75 payloadLines: [`payload = json.dumps(${formatPythonValue(parsed, 0)})`],
76 payloadVariable: 'payload',
77 }
78 } catch {
79 return {
80 payloadLines: [`payload = ${JSON.stringify(text)}`],
81 payloadVariable: 'payload',
82 }
83 }
84 }
85
86 if (mimeType === 'application/octet-stream' && text) {
87 return {
88 payloadLines: [`payload = ${formatBytesLiteral(text)}`],
89 payloadVariable: 'payload',
90 }
91 }
92
93 if (mimeType === 'application/x-www-form-urlencoded' && params) {
94 imports.add('import urllib.parse')
95 const formData: Record<string, string | string[]> = {}
96
97 params.forEach((param) => {
98 accumulateRepeatedValue(formData, param.name, param.value ?? '')
99 })
100
101 if (!headers['Content-Type']) {
102 headers['Content-Type'] = 'application/x-www-form-urlencoded'
103 }
104
105 return {
106 payloadLines: [`payload = urllib.parse.urlencode(${formatPythonValue(formData, 0)}, doseq=True)`],
107 payloadVariable: 'payload',
108 }
109 }
110
111 if (mimeType === 'multipart/form-data' && params) {

Callers 1

generateFunction · 0.70

Calls 3

formatPythonValueFunction · 0.90
accumulateRepeatedValueFunction · 0.90
formatBytesLiteralFunction · 0.85

Tested by

no test coverage detected