Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions pkg/engine/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,40 +109,49 @@ func (e *Engine) runOpenAPI(tool types.Tool, input string) (*Return, error) {
// Handle request body
if instructions.BodyContentMIME != "" {
res := gjson.Get(input, "requestBodyContent")
if res.Exists() {
var body bytes.Buffer
switch instructions.BodyContentMIME {
case "application/json":
if err := json.NewEncoder(&body).Encode(res.Value()); err != nil {
return nil, fmt.Errorf("failed to encode JSON: %w", err)
}
req.Header.Set("Content-Type", "application/json")
var body bytes.Buffer
switch instructions.BodyContentMIME {
case "application/json":
var reqBody interface{}

reqBody = struct{}{}
if res.Exists() {
reqBody = res.Value()
}
if err := json.NewEncoder(&body).Encode(reqBody); err != nil {
return nil, fmt.Errorf("failed to encode JSON: %w", err)
}
req.Header.Set("Content-Type", "application/json")

case "text/plain":
body.WriteString(res.String())
req.Header.Set("Content-Type", "text/plain")
case "text/plain":
reqBody := ""
if res.Exists() {
reqBody = res.String()
}
body.WriteString(reqBody)

case "multipart/form-data":
multiPartWriter := multipart.NewWriter(&body)
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
if res.IsObject() {
for k, v := range res.Map() {
if err := multiPartWriter.WriteField(k, v.String()); err != nil {
return nil, fmt.Errorf("failed to write multipart field: %w", err)
}
req.Header.Set("Content-Type", "text/plain")

case "multipart/form-data":
multiPartWriter := multipart.NewWriter(&body)
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
if res.Exists() && res.IsObject() {
for k, v := range res.Map() {
if err := multiPartWriter.WriteField(k, v.String()); err != nil {
return nil, fmt.Errorf("failed to write multipart field: %w", err)
}
} else {
return nil, fmt.Errorf("multipart/form-data requires an object as the requestBodyContent")
}
if err := multiPartWriter.Close(); err != nil {
return nil, fmt.Errorf("failed to close multipart writer: %w", err)
}

default:
return nil, fmt.Errorf("unsupported MIME type: %s", instructions.BodyContentMIME)
} else {
return nil, fmt.Errorf("multipart/form-data requires an object as the requestBodyContent")
}
if err := multiPartWriter.Close(); err != nil {
return nil, fmt.Errorf("failed to close multipart writer: %w", err)
}
req.Body = io.NopCloser(&body)

default:
return nil, fmt.Errorf("unsupported MIME type: %s", instructions.BodyContentMIME)
}
req.Body = io.NopCloser(&body)
}

// Make the request
Expand Down