MCPcopy
hub / github.com/intruder-io/autoswagger / build_request_body

Function build_request_body

autoswagger.py:352–385  ·  view source on GitHub ↗

Builds a request body based on the schema and specified content type. Supports JSON, XML, form-encoded, plain text, octet-stream, and multipart.

(schema, content_type, value_index=0)

Source from the content-addressed store, hash-verified

350 return None
351
352def build_request_body(schema, content_type, value_index=0):
353 """
354 Builds a request body based on the schema and specified content type.
355 Supports JSON, XML, form-encoded, plain text, octet-stream, and multipart.
356 """
357 if not schema:
358 return None
359
360 if 'oneOf' in schema or 'anyOf' in schema or 'allOf' in schema:
361 body = handle_composite_schemas(schema, value_index)
362 elif schema.get('type') == 'array':
363 item_schema = schema.get('items', {})
364 body = [build_array_item(item_schema, value_index)]
365 elif schema.get('type') == 'object':
366 body = build_nested_object(schema, value_index)
367 else:
368 param_type = schema.get('type', 'string')
369 enum = schema.get('enum', None)
370 values = generate_parameter_values(param_type, enum)
371 body = values[value_index % len(values)]
372
373 if content_type == 'application/x-www-form-urlencoded':
374 return urlencode(body)
375 elif content_type == 'application/xml':
376 return dicttoxml(body).decode()
377 elif content_type == 'application/json':
378 return json.dumps(body)
379 elif content_type == 'text/plain':
380 return str(body)
381 elif content_type == 'application/octet-stream':
382 return b'\x00\x01\x02'
383 elif content_type == 'multipart/form-data':
384 return build_file_upload_body(schema, content_type, value_index)
385 return json.dumps(body)
386
387def substitute_path_parameters(path, parameters, value_mapping):
388 """

Callers 1

test_endpointsFunction · 0.85

Calls 5

handle_composite_schemasFunction · 0.85
build_array_itemFunction · 0.85
build_nested_objectFunction · 0.85
build_file_upload_bodyFunction · 0.85

Tested by 1

test_endpointsFunction · 0.68