@@ -109,40 +109,49 @@ func (e *Engine) runOpenAPI(tool types.Tool, input string) (*Return, error) {
109
109
// Handle request body
110
110
if instructions .BodyContentMIME != "" {
111
111
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" )
120
125
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 )
124
132
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 )
133
142
}
134
- } else {
135
- return nil , fmt .Errorf ("multipart/form-data requires an object as the requestBodyContent" )
136
143
}
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 )
143
149
}
144
- req .Body = io .NopCloser (& body )
150
+
151
+ default :
152
+ return nil , fmt .Errorf ("unsupported MIME type: %s" , instructions .BodyContentMIME )
145
153
}
154
+ req .Body = io .NopCloser (& body )
146
155
}
147
156
148
157
// Make the request
0 commit comments