1
votes

how do I transform the CURL in Rest command ?

what is wrong

curl -X GET --header "Accept: application/json" --header "X-Authorization: Bearer "Token"" "http://console.agrolog.io:8080/api/auth/user"

Delphi code:

  RESTClient1.BaseURL := 'http://console.agrolog.io:8080/api/';

  RESTRequest1.Method := TRESTRequestMethod.rmGET;
  RESTRequest1.Resource := 'auth/user';

  RESTRequest1.Params.Clear;
  with RESTRequest1.Params.AddItem do
    begin
        ContentType := ctAPPLICATION_JSON;
        name        := 'Authorization';
        Value       := 'X-Authorization: Bearer ' + '"'+Strtoken+'"' ;
        Kind        :=  pkHTTPHEADER;
    end;

  RESTRequest1.Execute;

The error is EHTTPProtocolException with Message 'HTTP/1.1 401'

1
What is wrong with your code? Please provide error you receiving from API or from your program, and add more specific question. - Daniel Hornik
Not sure which error you get. I can only guess. But if your authentication doesn't work, then double check that you really need quotes around your bearer token. The format that I usually encounter is Bearer xxxxxxxxxxxxxxxxxx (without any quotes). - Wouter van Nifterick
The error is EHTTPProtocolException with Message 'HTTP/1.1 401' - user15105422

1 Answers

1
votes

You are setting up the REST parameter all wrong.

The parameter's Name should be 'X-Authorization' instead of 'Authorization' (why? 'Authorization' is a standardized header), and you need to remove the X-Authorization: prefix from its Value. You should also not be setting its ContentType at all.

Also, you need to set RESTRequest1.Accept to 'application/json'.

Try this:

RESTClient1.BaseURL := 'http://console.agrolog.io:8080/api/';

RESTRequest1.Method := TRESTRequestMethod.rmGET;
RESTRequest1.Resource := 'auth/user';
RESTRequest1.Accept := 'application/json';

RESTRequest1.Params.Clear;
with RESTRequest1.Params.AddItem do
begin
  Name := 'X-Authorization'; // 'Authorization'
  Value := 'Bearer "'+Strtoken+'"';
  Kind := pkHTTPHEADER;
end;

RESTRequest1.Execute;