Skip to content

Commit 20fc0e7

Browse files
Merge pull request #327 from cloudnautique/main
fix: handle empty request bodies
2 parents 1e50320 + 0a092ef commit 20fc0e7

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

pkg/engine/openapi.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,40 +109,49 @@ func (e *Engine) runOpenAPI(tool types.Tool, input string) (*Return, error) {
109109
// Handle request body
110110
if instructions.BodyContentMIME != "" {
111111
res := gjson.Get(input, "requestBodyContent")
112-
if res.Exists() {
113-
var body bytes.Buffer
114-
switch instructions.BodyContentMIME {
115-
case "application/json":
116-
if err := json.NewEncoder(&body).Encode(res.Value()); err != nil {
117-
return nil, fmt.Errorf("failed to encode JSON: %w", err)
118-
}
119-
req.Header.Set("Content-Type", "application/json")
112+
var body bytes.Buffer
113+
switch instructions.BodyContentMIME {
114+
case "application/json":
115+
var reqBody interface{}
116+
117+
reqBody = struct{}{}
118+
if res.Exists() {
119+
reqBody = res.Value()
120+
}
121+
if err := json.NewEncoder(&body).Encode(reqBody); err != nil {
122+
return nil, fmt.Errorf("failed to encode JSON: %w", err)
123+
}
124+
req.Header.Set("Content-Type", "application/json")
120125

121-
case "text/plain":
122-
body.WriteString(res.String())
123-
req.Header.Set("Content-Type", "text/plain")
126+
case "text/plain":
127+
reqBody := ""
128+
if res.Exists() {
129+
reqBody = res.String()
130+
}
131+
body.WriteString(reqBody)
124132

125-
case "multipart/form-data":
126-
multiPartWriter := multipart.NewWriter(&body)
127-
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
128-
if res.IsObject() {
129-
for k, v := range res.Map() {
130-
if err := multiPartWriter.WriteField(k, v.String()); err != nil {
131-
return nil, fmt.Errorf("failed to write multipart field: %w", err)
132-
}
133+
req.Header.Set("Content-Type", "text/plain")
134+
135+
case "multipart/form-data":
136+
multiPartWriter := multipart.NewWriter(&body)
137+
req.Header.Set("Content-Type", multiPartWriter.FormDataContentType())
138+
if res.Exists() && res.IsObject() {
139+
for k, v := range res.Map() {
140+
if err := multiPartWriter.WriteField(k, v.String()); err != nil {
141+
return nil, fmt.Errorf("failed to write multipart field: %w", err)
133142
}
134-
} else {
135-
return nil, fmt.Errorf("multipart/form-data requires an object as the requestBodyContent")
136143
}
137-
if err := multiPartWriter.Close(); err != nil {
138-
return nil, fmt.Errorf("failed to close multipart writer: %w", err)
139-
}
140-
141-
default:
142-
return nil, fmt.Errorf("unsupported MIME type: %s", instructions.BodyContentMIME)
144+
} else {
145+
return nil, fmt.Errorf("multipart/form-data requires an object as the requestBodyContent")
146+
}
147+
if err := multiPartWriter.Close(); err != nil {
148+
return nil, fmt.Errorf("failed to close multipart writer: %w", err)
143149
}
144-
req.Body = io.NopCloser(&body)
150+
151+
default:
152+
return nil, fmt.Errorf("unsupported MIME type: %s", instructions.BodyContentMIME)
145153
}
154+
req.Body = io.NopCloser(&body)
146155
}
147156

148157
// Make the request

0 commit comments

Comments
 (0)