0

I'm trying to Send a GET request to my api to obtain some result. i'm using x-www-form-urlencoded. I successfully get the result using the following curl:

curl -k -X GET https://localhost:8443/parser -H 'accept: */*' -H "Content-Type: application/x-www-form-urlencoded" -d "mydata=1"

the '-k' is because I'm using https.

However, when I'm using the following swagger in Nodejs

 * @swagger
 * /parser:
 *   get:
 *     summary: Get scenario validation result.
 *     requestBody:
 *       content:
 *         application/x-www-form-urlencoded:
 *           schema:
 *             type: object
 *             properties:
 *               mydata:
 *                 type: string
 *
 *     responses:
 *       200:
 *         description: success
 */

it returns an error TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

and the curl appeard as the follwoing :

curl -X 'GET' \
  'https://localhost:8443/parser' \
  -H 'accept: */*' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'mydata=8.1'

any hint?

VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

0

GET is not supposed to have a request body, and OpenAPI 3.0 Specification explicitly does not allow requestBody in GET, HEAD, and DELETE requests. (This restriction was later lifted in OAS 3.1 though.) When using GET, you can send data in the URL query string (http(s)://...?key1=value1&key2=value2&...) or request headers instead.

If you must use a request body (e.g. if the payload is huge or has a complex structure), use POST, PUT, or PATCH instead, depending on the request semantics.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Thanks for your answer! I'm sending in a Get Request a 4 kb text which is a language syntax to validate the lang and return the result so there is no need for post. My API is only configured with GET requests. What do you think should i do ? – user19640240 Aug 22 '22 at 11:25
  • Sounds like a typical use case for POST to me. Change your API to use POST instead of GET. Problem solved. – Helen Aug 22 '22 at 11:38