0

我正在尝试通过 REST 界面更新 Prestashop 中的制造商。到目前为止,我已经能够获取信息,但是当我尝试 PUT 时,我总是收到 HTTP 500 错误。

try
    Memo1.Text := '<prestashop><manufacturer><id><![CDATA[804]]></id><name><![CDATA[DisneyLand]]></name></manufacturer></prestashop>';

    RESTRequest1.Params.Clear;
    //authentication does not work with PUT, use token as suffix....
    RESTClient1.Authenticator := nil; //SimpleAuthenticator1;//
    //SimpleAuthenticator1.UserNameKey := 'ws_key';
    //SimpleAuthenticator1.UserName := 'HEREIGOESTHETOKEN';
    RESTRequest1.Resource := 'manufacturers';
    RESTRequest1.ResourceSuffix := '?ws_key=HEREIGOESTHETOKEN';
    RESTRequest1.Method := rmPut; //update
    RESTClient1.BaseURL := 'http://localhost/prestashop/api';
    RESTRequest1.Params.AddItem('id', '804' ,pkGETorPOST , [], ctTEXT_PLAIN);
    RESTRequest1.Params.AddItem('putXml', Memo1.Text,
                                pkGETorPOST , [], ctAPPLICATION_X_WWW_FORM_URLENCODED);

    RESTRequest1.Execute;

    Memo1.Lines.Clear;
    Memo1.Lines.Add(RESTResponse1.Content);
  except
    on E : Exception do
    begin
      Memo1.Lines.Clear;
      Memo1.Lines.Add('Exception class name = '+E.ClassName);
      Memo1.Lines.Add('Exception message = '+E.Message);
    end;
  end;

我尝试了其他 TRESTRequestParameterKind 可能性,但无济于事。

以前有人试过吗?

4

1 回答 1

0

更新:使用 Wireshark 监控我的网络后,我注意到如果我之前在任何地方使用 TSimpleauthenticer 组件,xml 总是附加 ws_key 值,导致 500 服务器错误。即使我清除所有 SimpleAuthenticator 设置并将客户端身份验证器设置为 nil,也会发生这种情况。

在正文中指定 xml 时,我还必须将内容类型设置为 ctTEXT_XML iso ctAPPLICATION_X_WWW_FORM_URLENCODED。

以下代码有效:

procedure TForm1.BtnNewMfgClick(Sender: TObject);       //new
var
  aNode, aCNode, aCCNode : IXMLNode;
  i,j : integer;
  aXml : string;
begin
  RESTRequest1.Params.Clear;
  RESTClient1.Authenticator := nil;
  SimpleAuthenticator1.UserNameKey := '';
  SimpleAuthenticator1.UserName := '';

  RESTClient1.BaseURL := 'http://localhost/prestashop/api';
  RESTRequest1.Resource := 'manufacturers';
  RESTRequest1.ResourceSuffix := '?schema=blank&ws_key=HEREGOESMYKEY';
  RESTRequest1.Method := rmGet;
  RESTRequest1.Execute;
  aXml := RESTResponse1.Content;
  XMLDocument1.LoadFromXML(aXml);

  aNode := XMLDocument1.ChildNodes.FindNode('prestashop');
  if assigned(aNode)
  then begin
    for i := 0 to aNode.ChildNodes.Count-1 do
    begin
      aCNode := aNode.ChildNodes.Get(i);
      for j := 0 to aCNode.ChildNodes.Count-1 do
      begin
        aCCNode := aCNode.ChildNodes.Get(j);
        if aCCNode.NodeName = 'id' then aCCNode.NodeValue := ''; //cannot pass id at create
        if aCCNode.NodeName = 'active' then aCCNode.NodeValue := '1' ;
        if aCCNode.NodeName = 'name' then aCCNode.NodeValue := 'New Brand';
      end;
    end;
  end;

  XmlDocument1.SaveToXML(aXml);

  RESTRequest1.ClearBody;
  RESTRequest1.AddBody(aXml, ctTEXT_XML);
  RESTRequest1.ResourceSuffix := '?ws_key=HEREGOESMYKEY';

  RESTRequest1.Method := rmPost;
  RESTRequest1.Execute;

  //new id is returned in the contents XML id tag
  Memo1.Lines.Clear;
  Memo1.Lines.Add(RESTResponse1.Content);
end;

虽然这在测试中有效,但我正在进一步调查它,因为在生产中它必须在 https:// 上工作,所以密钥不会暴露......

简而言之:Simpleauthenticator 仅适用于 GET 和 DELETE。一旦使用,PUT 和 POST 将永远无法工作。

于 2015-06-01T15:17:20.593 回答