2

I developed a very simple API using the Spark Framework (ref: sparkjava.com). I wrote a delete handler and tested it with Postman.

The Problem

When I try to assign a value using request.queryParams, a NULL value is assigned even though the data is in the HTTP request and shows up in request.body.

My Java:
delete("/threat", (req,res) -> {
    System.out.println("DELETE /threat");
    System.out.println("tid = "+ req.queryParams("tid"));
    res.status(200);
    return "done";
}); 
The Postman Request/Response
DELETE http://localhost:4567/threat 200 7ms
Request Headers
   Content-Type: application/x-www-form-urlencoded
   User-Agent: PostmanRuntime/7.28.4
   Accept: */*
   Postman-Token: 90615059-38c6-4901-9838-249b36c35887
   Host: localhost:4567
   Accept-Encoding: gzip, deflate, br
   Connection: keep-alive
   Content-Length: 40
Request Body
   tid: "839c9af2-9ab4-495d-883b-3cd509b8116d"
Response Headers
   Date: Wed, 15 Dec 2021 22:03:02 GMT
   Access-Control-Allow-Origin: *
   Content-Type: text/html;charset=utf-8
   Transfer-Encoding: chunked
   Server: Jetty(9.4.z-SNAPSHOT)
Response Body
   done
Console output
DELETE /threat
tid = null

If I output req.body() then it outputs tid=839c9af2-9ab4-495d-883b-3cd509b8116d, but it is strange that the output is null when trying to get req.queryParams("tid").

Testing I've done

If I use post() instead of delete() it works! The script outputs:

POST /threat
tid = 839c9af2-9ab4-495d-883b-3cd509b8116d
The question for the community

So before I go and write up my own body parser for the delete handler, can anybody tell me why req.queryParams("tid") returns null inside a delete() handler?

Research I've done

Googled, searched StackOverflow, read through the docs at SparkJava.com

Maven information (taken from my pom.xml)
<dependency>
   <groupId>com.sparkjava</groupId>
   <artifactId>spark-core</artifactId>
   <version>2.8.0</version>
</dependency>
Robert S
  • 496
  • 4
  • 14

1 Answers1

0

(Edit: thanks to @Aravinth for helping me find this information via their post: Swagger openApi Spec 3.0 - DELETE opeartion .)

Payloads on a DELETE are not supported. While this is not de jure, it appears Spark Java abides by not supporting payload retrieval via queryParams.

RFC7231 states:

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

(Source: https://www.rfc-editor.org/rfc/rfc7231#section-4.3)

Robert S
  • 496
  • 4
  • 14