简体   繁体   中英

Why HTTP GET with Request body in swagger & node js NOT working?

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?

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 (eg if the payload is huge or has a complex structure), use POST, PUT, or PATCH instead, depending on the request semantics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM