0

I have a Spring Boot REST endpoint written in Kotlin as such:

@GetMapping(value="/transferDetails")
private fun transferAccountDetails(@RequestBody transferAcctDetails: TransferAccountDetails):results{

    /** Hidden logic*/

    return results(true,"Details Transferred Successfully")
}

//region Data View classes for HTTP response are defined here
data class TransferAccountDetails(val ownerKey: String, val counterPartyKey: String, val acctId: String)

What the above endpoint does is merely taking the parameters from the request body and make some changes to the database and eventually return a success message.

Let say I cannot change the behavior or the request type used for this endpoint (e.g. POST or PathVariable) and that the above script is final and has been tested with Postman and it works.

On the frontend, I am using Angular and sending the request to the above endpoint in my service as follows:

  processAccountDetailsUrl = 'http://.../api/transferDetails';

  processAccountDetails(ownerKey: string, counterPartyKey: string, acctId: string) {
    this.paramsDict = {
      ownerKey: { ownerKey},
      counterPartyKey: { counterPartyKey},
      acctId: { acctId }
    };

    this.requestOptions = {
      params: new HttpParams(this.paramsDict)
    };

    return this.http.get(this.processAccountDetailsUrl , this.requestOptions);
  }

I am getting the following error 400:

{
    "timestamp": "2019-11-29T08:16:52.793+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Required request body is missing: private final com.template.webserver.results..."
    "path": "/api/transferAccountDetails"
}

Is requestOptions not the way to add request body to a GET request? If so, what changes should I make to be able to call the REST endpoint and where did it go wrong? Assume that I can only edit the frontend and the backend has been tested to be working and no further changes should be made.

I tried creating an interface in my TS file that has the same variables as the data class TransferAccountDetails and tried parsing it like return this.http.get(this.processAccountDetailsUrl , this.theInterfaceObject);. This is not working as it is not a type for a GET request.

kaiilim
  • 448
  • 6
  • 19

1 Answers1

1

It's not recommended to have a RequestBody on a get request :

HTTP GET with request body

https://github.com/angular/angular/issues/9927

Alann
  • 542
  • 3
  • 18
  • Yes, I understand that and have seen the post you have shared. But the scenario is that I cannot change what has been done on the backend. Is there a way to resolve this error for the frontend? – kaiilim Nov 29 '19 at 16:27
  • You can only change the front end not the back end ? – Alann Nov 29 '19 at 16:32
  • Yes, hypothetically, if that is the case then what did I do wrong on the angular side (my service.ts)? – kaiilim Nov 29 '19 at 16:52
  • well you can change the request body to requestParameter or pathVariable depending on what it's supposed to do – Alann Nov 29 '19 at 17:00
  • I am already using ```requestParameter``` (in the above ```service.ts``` script) isn't it (added ```requestParams``` in ```this.requestOptions```)? I can't use ```pathVariable``` on angular service because the endpoint requires a ```RequestBody``` right? – kaiilim Nov 29 '19 at 17:07
  • yeah right, if you can't change this request body, I'm not sure you will be able to reach your endpoint, maybe you can try to make an ajax call with another library, but it wouldn't be a good solution – Alann Nov 29 '19 at 17:09