how do I transform the CURL in Rest command Delphi

Viewed 246

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 Answers

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;
Related