简体   繁体   English

如何在 Delphi REST Datasnap 中声明 POST 请求?

[英]How to declare a POST request in Delphi REST Datasnap?

I'm trying to write a function that will insert informations in my database using JSON or multiple parameters in the URL.我正在尝试编写一个 function ,它将使用 JSON 或 URL 中的多个参数在我的数据库中插入信息。

I already managed to create a function that returns informations from my database to the client but not the other way.我已经设法创建了一个 function ,它将信息从我的数据库返回到客户端,但不是相反。 Every example I find explains how to creat a JSON but not how to receive it with System.JSON (Delphi 10.2)我找到的每个示例都说明了如何创建 JSON 但不说明如何使用 System.JSON 接收它(Delphi 10.2)

Here is a code sample for a GET function actually working on my Datasnap:这是实际在我的 Datasnap 上工作的 GET function 的代码示例:

function TDM_Test.DS_getArticles(pStock : String): string;
begin
  try
    VGOutils.PR_logs('Get all articles from table');

VGOutils.FU_CheckConnect('1234567890');

with VGOutils.SQ_temp1 do
begin
  SQL.Clear;
  SQL.Add('SELECT code, name, price, seller, departement FROM articles');
  SQL.Add('WHERE stock');
  ParamByName('stock').AsString := pStock;
  VGOutils.SQ_temp1.Open;
end;

GetInvocationMetadata().ResponseCode := 200;
GetInvocationMetadata().ResponseContent :=
  VGOutils.PR_JSON(VGOutils.SQ_temp1, ['code', 'name', 'price', 'seller']);

 except on E: Exception do
    PR_error(E.Message);
  end;

  VGOutils.PR_Disconnect;
end;

Now I need the client to send me new articles to INSERT in my database.现在我需要客户将新文章发送给我的数据库中的INSERT

The only thing that I cant figure out is how to declare the function and it's parameters.我唯一想不通的是如何声明 function 及其参数。

I want the client to be able to send me a JSON with this correct format我希望客户能够以这种正确的格式向我发送 JSON

{"article":[{"code":"AAA","name":"car","price" : "10400.90", "sellers" : [{"seller1" : "Automaniac", "seller2" : "Autopro" }]}]}

Now I know how to parse it with TJSONObject.ParseJSONValue() by reading this JSON RadStudio现在我知道如何通过阅读此JSON RadStudioTJSONObject.ParseJSONValue()解析它

EDIT If this JSON string is hardcoded it works fine with ParseJSONValue but if the string is taken from the URL my JSONstring only contains "]}" and obviously there is nothing to parse.编辑如果这个 JSON 字符串是硬编码的,它可以与 ParseJSONValue 一起正常工作,但如果字符串取自 URL 我的 JSONstring 只包含“]}”,显然没有什么可以解析的。

 function TDM_Test.DS_insertArticles(pUrlJsonString: String): string;

 // Hardcoded JSON string
 jsonString := '{"article":[{"code":"AAA","name":"car","price" : "10400.90", "sellers" : [{"seller1" : "Automaniac", "seller2" : "Autopro" }]}]}';
 JSonValue := TJSONObject.ParseJSONValue(jsonString);

 // String received in url
 jsonString := pUrlJsonString;
 JSonValue := TJSONObject.ParseJSONValue(pUrlJsonString);

I found the way I wanted it to work by not using the string parameter of the function but instead using the Custom body by passing a JSONObject.通过不使用 function 的字符串参数,而是通过传递 JSONObject 来使用Custom body ,我找到了我希望它工作的方式。 The method is POST and the content type is application/JSON .方法是POST ,内容类型是application/JSON

function TDM_Test.DS_insertArticles(pUrlJsonObj: TJSONObject): string;

JSonValue := TJSONObject.ParseJSONValue(pUrlJsonObj);

// Retrieve data for database fields
artCode  := JSONValue.GetValue<string>('article[0].code');
artName  := JSONValue.GetValue<string>('article[0].name');
artPrice := JSONValue.GetValue<string>('article[0].price');

//... Proceed to INSERT SQL with these values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM