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>