Delphi: idHTTP.Post HTTP error/1.1 401

Asked

Viewed 2,972 times

0

I’m trying to access Delphi’s idHTTP on a json server without success. I have tried all alternatives and always have the same error: "HTTP/1.1 401 Unauthorized".

Json format submitted for testing:

{"http":{"method":"POST","header":"access_token:55b3ce85b47629ee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636", "content":"name=Teste&[email protected]&phone=1147001211&mobilePhone=11992329909&address=Rua+Jose+Ricardo &addressNumber=55&Province=Bairro&notificationdisabled=True&city=Sao+Paulo&state=Sp&country=Brazil&postalcode=05567210 &cpfCnpj=11111111111&personType=FISICA"}}

Url for testing:

http://homolog.asaas.com/api/v2/customers

Procedure used for testing:

procedure TForm4.Button2Click(Sender: TObject);
var
 sResponse: string;
 EnvStr : TStringList;
begin
 // 2
 EnvStr := TStringList.Create;
 EnvStr.AddStrings(Memo.Lines);
 try
  idHTTP.Request.ContentType := 'application/json';
  idHTTP.Request.Method:='POST';
  idHTTP.Request.AcceptCharSet := 'utf-8';
  try
   sResponse := idHTTP.Post(EditURL.Text,EnvStr);
  except
   on E: Exception do
     ShowMessage('Error on request: '#13#10 + e.Message);
  end;
finally
 MemoRet.Lines.Clear;
 MemoRet.Lines.add(sResponse);
end;

end;

The same format sent in PHP works perfectly, but with idHTTP returns the error: "HTTP/1.1 401 Unauthorized".

Does anyone already do this procedure in Delphi with asaas.com? Thank you in advance!

1 answer

1


User Thanks Remy Lebeau (https://stackoverflow.com/users/65863/remy-lebeau), by the solution sent in English!

procedure TForm4.Button2Click(Sender: TObject);
var
 sResponse: string;
 EnvStr : TStringList;
begin
 EnvStr := TStringList.Create;
 try
  EnvStr.Add('name=TEST');
  EnvStr.Add('[email protected]');
  EnvStr.Add('phone=1147001211');
  EnvStr.Add('mobilePhone=11992329909');
  EnvStr.Add('address=Rua Jose Ricardo ');
  EnvStr.Add('addressNumber=55');
  EnvStr.Add('province=Test');
  EnvStr.Add('notificationDisabled=True');
  EnvStr.Add('city=Sao Paulo');
  EnvStr.Add('state=SP');
  EnvStr.Add('country=Brasil');
  EnvStr.Add('postalCode=05567210 ');
  EnvStr.Add('cpfCnpj=11111111111');
  EnvStr.Add('personType=FISICA');

  Http.Request.CustomHeaders.Values['access_token'] := '55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636';
  try
    sResponse := idHTTP.Post(EditURL.Text, EnvStr);
  except
    on E: Exception do
      ShowMessage('Error on request: '#13#10 + e.Message);
  end;
 finally
   EnvStr.Free;
   MemoRet.Text := sResponse;
 end; 
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.