Delphi: idHTTP.Post HTTP / 1.1 error 401

0

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

Json format submitted for testing:

  

{"http": {"method": "POST", "header": "access_token: 55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636",   "content": "name = TEST & email = [email protected] & phone = 1147001211 & mobilePhone = 11992329909 & address = Street + Jose + Ricardo   & addressNumber = 55 & province = Neighborhood & notificationDisabled = True & city = Sao + Paulo & State = SP & country = Brazil & postalCode = 05567210   & cpfCnpj = 11111111111 & personType = PHYSICS "}}

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 it returns the error: "HTTP / 1.1 401 Unauthorized."

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

    
asked by anonymous 28.01.2016 / 15:04

1 answer

1

Thanks to user Remy Lebeau ( link ) for 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;
    
30.01.2016 / 22:42