-3

source gives socket error 14001, WITH OBJ JSON PARAM MESSAGE FOR POST

jso := TlkJSONobject.Create; // (data) as TlkJSONobject;   

jso.Add('InvoiceNumber', '');
jso.Add('POSID', '910441');
jso.add('USIN', ePOSNo.Text);
jso.add('DATETIME', eDate.Text);

IdHTTP1.Request.Accept := 'application/json';
IdHTTP1.Request.ContentType := 'application/json';

{ Call the Post method of TIdHTTP and read the result into TMemo }
Memo1.Lines.Text := IdHTTP1.Post('http://localhost;8524/api/IMSFISCAL/GetInvoiceNumberByModel', JSO);

json cannot be passed as tstream

need help on it

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hamza
  • 33
  • 6

1 Answers1

3

There is no way the code you showed can produce a socket error (let alone error 14001, which is not even a socket error) since the code won't even compile!

The TIdHTTP.Post() method does not have an overload that accepts a TlkJSONobject as input. How could it? TlkJSONobject comes from a completely different 3rd party library, it is not part of the RTL or Indy. The only things you can POST with TIdHTTP are:

  • TStrings-derived types
  • TStream-derived types, including Indy's TIdMultiPartFormDataStream
  • a file specified by a String filename

In this case, you need to use a TStream to post JSON stored in memory. It is your responsibility to save your TlkJSONobject content to a suitable TStream of your choosing. That is outside the scope of Indy. For instance, you can use TlkJSON.GenerateText() to get the JSON into a String and then POST it using a TStringStream.

On a side note, the URL you are passing to TIdHTTP.Post() is malformed. The correct delimiter between a hostname and port number is a colon (:), not a semicolon (;).

With that said, try this:

jso := TlkJSONobject.Create;

jso.Add('InvoiceNumber', '');
jso.Add('POSID', '910441');
jso.add('USIN', ePOSNo.Text);
jso.add('DATETIME', eDate.Text);

IdHTTP1.Request.Accept := 'application/json';
IdHTTP1.Request.ContentType := 'application/json';

{ Call the Post method of TIdHTTP and read the result into TMemo }
PostData := TStringStream.Create(TlkJSON.GenerateText(jso), TEncoding.UTF8);
try
  Memo1.Lines.Text := IdHTTP1.Post('http://localhost:8524/api/IMSFISCAL/GetInvoiceNumberByModel', PostData);
finally
  PostData.Free;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I'll remember this answer for my future work. +1), thanks. – Zhorov Jan 10 '20 at 08:50
  • :i m gettting error #10061 now with connection refused – Hamza Jan 10 '20 at 12:49
  • @Hamza did you fix the malformed URL like I described? If so, then the error means you likely do not have an HTTP server running on `localhost` (127.0.0.1) on port 8524 – Remy Lebeau Jan 10 '20 at 15:19
  • jso: TlkJSONobject; PostData: TstringStream;begin jso := TlkJSONobject.Create; jso.Add('InvoiceNumber', ''); jso.Add('POSID', '910441'); jso.add('USIN', ePOSNo.Text); jso.add('DATETIME', eDate.Text); IdHTTP1.Request.Accept := 'application/json'; IdHTTP1.Request.ContentType := 'application/json'; { Call the Post method of TIdHTTP and read the result into TMemo } PostData := TStringStream.Create(TlkJSON.GenerateText(jso)); try Memo1.Lines.Text := IdHTTP1.Post('http://localhost:8524/api/IMSFISCAL/GetInvoiceNumberByModel', PostData); finally PostData.Free; end; – Hamza Jan 10 '20 at 17:14
  • @Hamza assuming your URL actually starts with `http://` and my browser is not just hiding it, then the code is fine (which version of Delphi are you using?). So again, are you actually running an HTTP server on your local machine, listening on 127.0.0.1 on port 8524? – Remy Lebeau Jan 10 '20 at 17:48
  • iis is running on pc and fiscalservice is running in pc – Hamza Jan 10 '20 at 19:56
  • its post call to webservice api running on some other system ,https://help.fbr.gov.pk/?p=6148 – Hamza Jan 10 '20 at 20:12