Problems Using httpclient lazarus / 61-Beginners ---------------------------------------------------------------------- kevtri · 2020-01-19 14:10 I am learning to use FPC's fphttpclient to POST a JSON text string to a localhost webserver listening on port 3000. I can do this through CURL and Postman, so I know the local webserver is listening correctly. But I don't seem to be able to send the JSON string using FPC httpclient with the code below. Am I missing something simple below. Any help would be greatly appreciated. Code: [Select]unit Unit1; interface uses fphttpclient; procedure Tlogin (const url: string); var   Result: boolean;   Ms: TMemoryStream;   HTTPClient: TFPHTTPClient; begin   Ms := TMemoryStream.Create;   try     HTTPClient := TFPHTTPClient.Create(nil);     try       HTTPClient.AllowRedirect := True;       HTTPClient.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');       HTTPClient.AddHeader('Content-Type', 'application/json');       HTTPClient.RequestBody := TStringStream.Create ('{"loginid":"user1","password":"12345abc"}');       HTTPClient.Post(url);       if HTTPClient.ResponseStatusCode = 200 then       begin                 end;     except       Result := False;     end;   finally     Ms.Free;   end; end;    begin     Tlogin ('http://localhost:3000/login'); end. ---------------------------------------------------------------------- lainz · 2020-01-19 14:30 Maybe missing Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HTTPClient.AddHeader('Accept', 'application/json'); ---------------------------------------------------------------------- jamie · 2020-01-19 14:35 would be interesting to know the response code that actually returned Could be a SSL connection issue.. etc.. ---------------------------------------------------------------------- lucamar · 2020-01-19 16:47 Quote from: lainz on January 19, 2020, 03:30:00 pmMaybe missing Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HTTPClient.AddHeader('Accept', 'application/json'); That would only be relevant if the server returned a json-formatted  response. Jamie has actually hit the nail: what's the actual response (if any) of the server? You should at least change your test to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       if HTTPClient.ResponseStatusCode = 200 then      begin        {Do something}        end else        with HTTPClient do          ShowMessageFmt('%d - %s', [ResponseStatusCode, ResponseStatusText]); and something alike on your exception handler. ---------------------------------------------------------------------- kevtri · 2020-01-19 22:20 Thanks all for your help. Much appreciated. 1. I added HTTPClient.AddHeader('Accept', 'application/json'); And outputted the returned message as: > The returned message is 500 - internal server error. 2. If I remove "Content-Type" and only have "Accept", then I get > 415 - Unsupported Media Type Code: [Select]      {HTTPClient.AddHeader('Content-Type', 'application/json');}       HTTPClient.AddHeader('Accept', 'application/json'); 3. In Postman, the only headers I have is: [{"key":"Content-Type","value":"application/json","description":"","ty pe":"text","enabled":true}] And the body is just the raw JSON: {"loginid":"user1","password":"12345abc"} 4. The CURL is: Code: [Select]curl --location --request POST 'http://localhost:3000/login' \ --header 'Content-Type: application/json' \ --data-raw '{"loginid":"user1","password":"12345abc"}' This CURL also works: Code: [Select]curl -v -H "Content-type: application/json" -X POST -d '{"loginid":"user1","password":"12345abc"}' http://localhost:3000/login ---------------------------------------------------------------------- lainz · 2020-01-20 00:09 If the error  is 500 if you have access try to see the logs of the server Try adding a user agent sometimes that solves the problem ---------------------------------------------------------------------- kevtri · 2020-01-20 01:16 Lainz, That's it. The problem was that it was rejecting the agent. You are right. I just blanked out that line of setting the agent to mozilla fpc and the server accepted it. Thanks everyone for all your help. ---------------------------------------------------------------------- PaulRowntree · 2020-01-20 05:11 Why did the curl work? ---------------------------------------------------------------------- trev · 2020-01-20 05:40 I assume curl worked because it sends a default user-agent eg "User- Agent: curl/7.67.0" if you don't specify one manually.