Delphi Seattle - 使用自定义正文发送发布请求响应错误请求400

时间:2018-05-30 08:32:08

标签: rest delphi post delphi-10-seattle

我想向外部提供商的Rest服务器发送一个发布请求。我尝试过Curl,一切都很完美。这是CURL代码:

    curl -X POST -H 'PRODUCT-KEY: SUperL0ngAndSpecialSecretCode' -H 'Content-Type: application/json' -H 'Authorization: Basic CrytedWorksalsowellwithotherget' -i 'http://myserver:8080/rest.svc/v1.0/query' --data '{
"query":"SELECT Name from Address where name like '\''%me%'\''"
}'

In Curl一切正常。 我已经尝试了一段时间让这个代码在Delphi中运行。这时我的代码看起来像这样:

function GetSomeInformation: string;
var
  lrestrequest: TRESTRequest;
  lRestClient: TRESTClient;
  lRestResponce: TRESTResponse;
begin
  result := '';
  lRestClient := TRESTClient.Create('http://myserver:8080/rest.svc/v1.0/query');
  try
    lrestrequest := TRESTRequest.Create(nil);
    try
      lRestResponce := TRESTResponse.Create(nil);
      try
        lrestrequest.Client := lRestClient;
        lrestrequest.Response := lRestResponce;
        lrestrequest.Method := rmPost;
        lrestrequest.Params.AddItem('PRODUCT-KEY',
          'SUperL0ngAndSpecialSecretCode',
          TRESTRequestParameterKind.pkHTTPHEADER);
        lrestrequest.Params.AddItem('Content-Type', 'application/json',
          TRESTRequestParameterKind.pkHTTPHEADER);
        lrestrequest.Params.AddItem('query',
          ansitoutf8('SELECT Name from Address where Name like ' +
          quotedstr('%me%')), TRESTRequestParameterKind.pkREQUESTBODY);
        lrestrequest.Execute;
        if not lRestResponce.Status.Success then
          showmessage(lRestResponce.StatusText + ' ' +
            inttostr(lRestResponce.StatusCode))
        else
          result := lRestResponce.Content;
      finally
        lRestResponce.Free;
      end;
    finally
      lrestrequest.Free
    end;
  finally
    lRestClient.Free;
  end;
end;

我不知道接下来要做什么工作?我可以更好地调试问题的任何想法或方法。

- 更新

好的,我使用Wireshark检查Post命令之间是否有任何差异,看起来像Delphi忽略或破坏了我的Header。 在Wireshark片段中有一个Value Content-Type。它应该是

Content-Type: application/json

但是使用Delphi我得到了

Content-Type: application%2Fjson, application/x-www-form-urlencoded

我也错过了Procduct-Key Value。 有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您必须在TRESTRequest

的Method属性中指定请求的类型
  

lRESTRequest.Method:= TRESTRequestMethod.rmPost

Delphi附带代码示例!

http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/REST.RESTDemo_Sample

答案 1 :(得分:0)

经过一些研究和一些Wireshark后,我至少完成了我的工作,这就是我将参数输入我的请求的方式。

function GetSomeInformation: string;
var
  lrestrequest: TRESTRequest;
  lRestClient: TRESTClient;
  lRestResponce: TRESTResponse;
begin
  result := '';
  lRestClient := TRESTClient.Create('http://myserver:8080/rest.svc/v1.0/query');
  try
    lrestrequest := TRESTRequest.Create(nil);
    try
      lRestResponce := TRESTResponse.Create(nil);
      try
        lrestrequest.Client := lRestClient;
        lrestrequest.Response := lRestResponce;
        RESTRequest1.Params.Clear;
        RESTRequest1.Method:=rmpost;
        RESTResponse1.RootElement := '';
        lparam := RESTRequest1.Params.AddItem;
        lparam.name := 'PRODUCT-KEY';
        lparam.Value := 'SpecialKeyButWithSomeTrickyCharsLike==';
        lparam.ContentType := ctNone;
        lparam.Kind := pkHTTPHEADER;
        //This one is Important otherwise the '==' will get url encoded
        lparam.Options := [poDoNotEncode];

        lparam := RESTRequest1.Params.AddItem;
        lparam.name := 'data';
        lparam.Value := '{"query":"' + SelectString + '"}';
        lparam.ContentType := ctAPPLICATION_JSON;
        lparam.Kind := pkGETorPOST;
        lrestrequest.Execute;
        if not lRestResponce.Status.Success then
          showmessage(lRestResponce.StatusText + ' ' +
            inttostr(lRestResponce.StatusCode))
        else
          result := lRestResponce.Content;
      finally
        lRestResponce.Free;
      end;
    finally
      lrestrequest.Free
    end;
  finally
    lRestClient.Free;
  end;
end;

感谢您的支持!

PJM