【发布时间】:2022-01-13 03:46:25
【问题描述】:
我在 Spring 中构建了一个 REST API,目前运行良好。我现在想在我的请求中添加一个带有数据的正文。我的 REST API 端点等待请求中的正文数据,如下所示。
@RestController
public class TestController {
@GetMapping("/test")
public String Test(@RequestBody(required=true) String fullName) {
return "Hello " + fullName;
}
}
我尝试通过命令行调用端点,如下所示。
curl -X GET -H "Content-type: application/json" -d "John Doe" "http://localhost:8080/test"
这会产生以下结果,并证明 REST API 工作正常。
Hello John Doe
无论如何我都无法在 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;
有谁知道我如何做到这一点?我非常感谢任何形式的帮助,纯粹的!
我尝试以多种不同的方式调用端点,如下所示。
// see above...
RESTRequest.ClearBody;
RESTRequest.Params.AddHeader('Content-Type', 'application/json');
RESTRequest.Body.Add('{"fullname": "John Doe"}');
RESTRequest.Execute;
很遗憾,这会导致以下错误。
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Invalid mime type "application%2Fjson": does not contain '/']
【问题讨论】:
标签: json spring rest delphi http-post