Recent

Author Topic: [SOLVED] rest idhttp and json raw  (Read 3335 times)

superc

  • Sr. Member
  • ****
  • Posts: 251
[SOLVED] rest idhttp and json raw
« on: February 28, 2022, 11:49:24 am »
Hello,

from postman I use an API rest passing parameter in json raw mode with no problem; in my program I use method POST with no problem passing parameter in a TStringList,
but how do i pass the parameters in GET?

Thanks in advance
« Last Edit: March 03, 2022, 10:58:27 am by superc »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1576
    • Lebeau Software
Re: rest idhttp and json raw
« Reply #1 on: February 28, 2022, 07:42:24 pm »
but how do i pass the parameters in GET?

A GET request doesn't have any input parameters in its body, only URL query parameters.

A JSON document needs to be sent using a POST or PUT request instead (depending on API).  However, TStringList is the wrong way to send JSON data, use a TStream instead, such as TStringStream.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

superc

  • Sr. Member
  • ****
  • Posts: 251
Re: rest idhttp and json raw
« Reply #2 on: March 02, 2022, 12:31:37 pm »
I saw this post where the tstringlist is used

https://stackoverflow.com/questions/19892471/idhttp-indy-post-do-request-parameters-with-utf-8

I use POST with Tstringlist in 'x-www-form-urlencoded' request
« Last Edit: March 02, 2022, 12:34:19 pm by superc »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1576
    • Lebeau Software
Re: rest idhttp and json raw
« Reply #3 on: March 02, 2022, 05:39:33 pm »
I saw this post where the tstringlist is used
...
I use POST with Tstringlist in 'x-www-form-urlencoded' request

That example is not posting JSON data, though.  It is posting 'name=value' pairs in 'x-www-form-urlencoded' format.  The TStrings overload of TIdHTTP.Post() is designed for that format.  But, you cannot post JSON data in that format.  You must send the raw JSON bytes using a TStream, and you must set the Request.ContentType to a suitable JSON media type.  For example:

Code: Pascal  [Select][+][-]
  1. var
  2.   PostData: TStringStream;
  3. begin
  4.   PostData := TStringStream.Create('{"json":"here"}', TEncoding.UTF8);
  5.   try
  6.     IdHTTP1.Request.ContentType := 'application/json';
  7.     IdHTTP1.Post(url, PostData);
  8.   finally
  9.     PostData.Free;
  10.   end;
  11. end;
  12.  
« Last Edit: March 03, 2022, 05:23:00 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

superc

  • Sr. Member
  • ****
  • Posts: 251
Re: rest idhttp and json raw
« Reply #4 on: March 03, 2022, 10:58:12 am »
Now everything is clear, thank you again

 

TinyPortal © 2005-2018