Issue
I've built a REST API in Spring, which is working well so far. I now want to add a body with data to my request. My REST API endpoint, which awaits the body data in a request looks like the following.
@RestController
public class TestController {
@GetMapping("/test")
public String Test(@RequestBody(required=true) String fullName) {
return "Hello " + fullName;
}
}
I've tried to call the endpoint via command line, like down below.
curl -X GET -H "Content-type: application/json" -d "John Doe" "http://localhost:8080/test"
This results the following and proves that the REST API is working fine.
Hello John Doe
Anyways I couldn't get it done in Delphi.
procedure TForm1.Button1Click(Sender: TObject);
var
RESTClient : TRESTClient;
RESTRequest : TRESTRequest;
RESTResponse : TRESTResponse;
begin
RESTClient := TRESTClient.Create(nil);
RESTClient.BaseURL := 'http://localhost:8080/test';
RESTResponse := TRESTResponse.Create(nil);
RESTRequest := TRESTRequest.Create(nil);
RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
RESTRequest.Execute;
if RESTResponse.Status.Success then
begin
ShowMessage(RESTResponse.Content);
end;
end;
Does anybody knows how I can achieve this? I highly appreciate any kind of help, sheers!
I've tried to call the endpoint in many different variations, one shown below.
// see above...
RESTRequest.ClearBody;
RESTRequest.Params.AddHeader('Content-Type', 'application/json');
RESTRequest.Body.Add('{"fullname": "John Doe"}');
RESTRequest.Execute;
Sadly, this results in the following error.
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Invalid mime type "application%2Fjson": does not contain '/']
Solution
My naive approach would be:
RESTRequest.ClearBody;
RESTRequest.AddBody('John Doe');
RESTRequest.Execute;
That will make a plain string body. If you actually need a JSON body you can write
RESTRequest.AddBody('{"fullname": "John Doe"}', TRESTContentType.ctAPPLICATION_JSON);
but as I am not familiar with Spring I am unsure if that fits here.
Update: After you changed your server to accept JSON objects the second approach seems to be the better one. In that case you can even make use of a similar Delphi class.
type
TPerson = class
private
FFirstname: string;
FLastname: string;
public
property Firstname: string read FFirstname write FFirstname;
property Lastname: string read FLastname write FLastname;
end;
person := TPerson.Create;
try
person.Firstname := 'John';
person.Lastname := 'Doe';
RESTRequest.AddBody<TPerson>(person);
finally
person.Free;
end;
Answered By - Uwe Raabe
Answer Checked By - Willingham (JavaFixing Volunteer)