diff --git a/pkg/engine/openapi.go b/pkg/engine/openapi.go index e2de8b6a..e872b7d4 100644 --- a/pkg/engine/openapi.go +++ b/pkg/engine/openapi.go @@ -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