简体   繁体   中英

I have problem about TRestRequest in delphi

I have a code for connect the RestAPI Server. This code made 3 components that TRestCLient , TRestRequest , TRestResponse . And i will try to get API response. However, one server has no problem but the other server occurs error about invalid parameter.( ENetHTtpException with message: 'Error querying headers: (87)... ) The RESTAPI services is same on both servers. And The error was not occur on the delphi REST Debugger...

As a result part of error is on my code.. but i can not discover the point..

  1. I tried to setting like to Rest Debugger component. -> this fail.
  2. I traced for ' DoExecuteRequest ' of ' System.Net.HttpClient.Win ' by debugging.
procedure TRestAPI.SetAPIInfo(EnabledKey, EnabledUnit: Boolean);
var
  Param: pAPIParameter;
  i, iIndex: Integer;
begin
  if (FMethodType = '') or (FResource = '') then
    Exit;

  FRRp.ResetToDefaults;
  FRRq.ResetToDefaults;

  FRRp.RootElement := '';
  FRRq.Params.CreateURLSegmentsFromString(FResource);
  FRRq.Method := RESTRequestMethodFromString(FMethodType);
  FRRq.Resource := FResource;
  FRRq.Params.Clear;

  if EnabledKey then
  begin
    FRRq.Params.AddItem;
    FRRq.Params[0].Name  := PARAM_KEY_AUTH;
    FRRq.Params[0].Value := PARAM_KEY_BEARER + ' ' + FUserKey;
    FRRq.Params[0].Kind  := pkHTTPHEADER;
    FRRq.Params[0].Options := FRRq.Params[0].Options + [poDoNotEncode];
  end;

  if EnabledUnit then
  begin
    FRRq.Params.AddItem;
    FRRq.Params[1].Name  := PARAM_KEY_UNIT_ID;
    FRRq.Params[1].Value := IntToStr(FUnitID);
    FRRq.Params[1].Kind  := pkHTTPHEADER;
    FRRq.Params[1].Options := FRRq.Params[1].Options + [poDoNotEncode];
  end;

  iIndex := FRRq.Params.Count;
  if FParamList.Count > 0 then
  begin
    for I := 0 to FParamList.Count-1 do
    begin
      Param := FParamList.Items[i];
      FRRq.Params.AddItem;
      FRRq.Params[iIndex + i].Name  := Param^.Name;
      FRRq.Params[iIndex + i].Value := Param^.Value;
      FRRq.Params[iIndex + i].Kind  := Param^.Kind;
      FRRq.Params[iIndex + i].Options := FRRq.Params[iIndex + i].Options + [poDoNotEncode];
    end;
  end;
end;

function TRestAPI.ExcuteAPI(EnabledKey, EnabledUnit, ArrayOfResult: Boolean;
  ArrayDSField: array of TDSFieldInfo): Boolean;
begin
  Result := False;
  FErrCode := -1;
  FErrText := '';
  FRecordCount := -1;
  FAPIResponse := '';

  SetAPIInfo(EnabledKey, EnabledUnit);
  try
    FRRq.Execute;
  except
    on E:Exception do
    begin
      SetResultCodeText(999, 'Check the RestAPI.' + NL + e.Message);
      Exit;
    end;
  end;

  FAPIResponse := FRRp.Content;
  if (FRRp.StatusCode = 200) then
  begin
    if not SetDataSetField(ArrayDSField) then Exit;
    if not JsonToDataSet(ArrayOfResult, FRRp.Content, ArrayDSField) then
    begin
      ClearDataSetField;
      Exit;
    end;
    FCDS.First;
    FRecordCount := FCDS.RecordCount;
  end
  else if FRRp.StatusCode >= 300 then
  begin
    SetResultCodeText(FRRp.StatusCode, FRRp.StatusText + NL + FRRp.Content);
    Exit;
  end;

  Result := True;
end;

Exception class ENetHTTPException with message 'Error querying headers: (87) The parameter is incorrect.

The fault is in my program.

I need the bearer-key to post for the REST API. and my program is on multi-thread environment. I got the key by one-thread and used the key for each thread. That was the cause. I changed the code to get the key from each thread.

That's all. I don't know why this error occur. But I knew the fact that the 'Cookie' of one of response header is missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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