1

I know how to send a post request using THTTPClient, but how to post json? my code:

 var
      http: THTTPClient;
      params: TStringList;
    begin
      http:= THTTPClient.Create;
      params:= TStringList.Create;
      params.Add('login=admin');
      params.Add('password=123456');
      http.Post('http://localhost/login', params);

my json:

{
    "login": "admin",
    "password": "123456"
}
Michael
  • 83
  • 2
  • 9
  • Have you seen – Raouf Rahiche Mar 16 '18 at 13:54
  • @RaoufRahiche yes, i have. there is different component – Michael Mar 16 '18 at 13:58
  • @Michael: If you [read the documentation](http://docwiki.embarcadero.com/Libraries/en/System.Net.HttpClient.THTTPClient.Post), `THTTPClient.Post()` has several overloads, one of which can post a `TStream`, just like the `TIdHTTP.Post()` method can in the example that Raouf pointed to. A lot of Embarcadero's HTTP and REST framework design is inspired by Indy's components, since that is what Embarcadero used to use before venturing out with their own frameworks. – Remy Lebeau Mar 16 '18 at 17:37

1 Answers1

1

You have to be sure that the URL you are posting accepts json on posts and you also have to specify in a header that you want to send a JSON to the URL.

the header is:

Content-Type:application/json
Gustavo
  • 86
  • 9