Recent

Author Topic: Problems Using httpclient  (Read 2051 times)

kevtri

  • Newbie
  • Posts: 3
Problems Using httpclient
« on: January 19, 2020, 03:10:08 pm »
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

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Problems Using httpclient
« Reply #1 on: January 19, 2020, 03:30:00 pm »
Maybe missing

Code: Pascal  [Select][+][-]
  1. HTTPClient.AddHeader('Accept', 'application/json');


jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problems Using httpclient
« Reply #2 on: January 19, 2020, 03:35:19 pm »
would be interesting to know the response code that actually returned

Could be a SSL connection issue.. etc..
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Problems Using httpclient
« Reply #3 on: January 19, 2020, 05:47:27 pm »
Maybe missing
Code: Pascal  [Select][+][-]
  1. 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][+][-]
  1.       if HTTPClient.ResponseStatusCode = 200 then
  2.       begin
  3.         {Do something}  
  4.       end else
  5.         with HTTPClient do
  6.           ShowMessageFmt('%d - %s', [ResponseStatusCode, ResponseStatusText]);

and something alike on your exception handler.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

kevtri

  • Newbie
  • Posts: 3
Re: Problems Using httpclient
« Reply #4 on: January 19, 2020, 11:20:21 pm »
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":"","type":"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
« Last Edit: January 19, 2020, 11:33:58 pm by kevtri »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Problems Using httpclient
« Reply #5 on: January 20, 2020, 01:09:20 am »
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
« Last Edit: January 20, 2020, 01:12:09 am by lainz »

kevtri

  • Newbie
  • Posts: 3
Re: Problems Using httpclient
« Reply #6 on: January 20, 2020, 02:16:26 am »
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

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Problems Using httpclient
« Reply #7 on: January 20, 2020, 06:11:47 am »
Why did the curl work?
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Problems Using httpclient
« Reply #8 on: January 20, 2020, 06:40:12 am »
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.

 

TinyPortal © 2005-2018