fphttpclient and json lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- dbannon · 2020-08-25 09:58 I am using a fphttpclient to sent a POST to a NextCloud server (apache/php/nextcloud) hitting a particular api that needs json.  I do (simplified form) - 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 var    Client: TFPHttpClient;    Response : TStringStream;    Params : string = '{"title":"Some New Note","content":"This is stuff in the new note",}';begin    Client := TFPHttpClient.Create(nil);    Client.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');    Client.AddHeader('Content-type','application/json; charset=UTF-8');  Client.AddHeader('Accept', 'application/json');    Client.AllowRedirect := true;    Client.UserName:=USER;    Client.Password:=PW;    Response := TStringStream.Create('');    Client.FormPost(TheURL,  Params, Response);     However, while the POST succeeds (and returns 200) the application at the end seems to ignore the payload, the little json string in Params.  When it arrives at the server, apache logs show (again, simplified) - Code: [Select]dumpio_in (data-HEAP): POST /nextcloud/index.php/apps/notes/api/v1/notes HTTP/1.1\r\n dumpio_in (data-HEAP): Authorization: Basic ZGJhbm5vbjpEYXZvc09sZFBX\r\n dumpio_in (data-HEAP): Host: 192.168.1.137\r\n dumpio_in (data-HEAP): Content-Type: application/x-www-form- urlencoded\r\n dumpio_in (data-HEAP): Accept: application/json\r\n dumpio_in (data-HEAP): Connection: close\r\n dumpio_in (data-HEAP): {"title":"Some New Note","content":"This is stuff in the new note"} As you can see, despite my setting of Content-Type = application/json it appears here as application/x-www-form-urlencode but the content itself is not in fact encoded ??   Am I setting this up in an appropriate manner ? Davo ---------------------------------------------------------------------- dbannon · 2020-08-26 00:09 So, just a touch more info, using a curl command line, I can get the behaviour I expect, the difference is (in the apache logs) that the curl line declares the content to be json - 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 dumpio_in (data-HEAP): Content-Type: application/json\r\n but the httpclient insists its www-form-urlencoded - 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 dumpio_in (data-HEAP): Content-Type: application/x-www-form-urlencoded\r\n How can I force httpclient to send as json as requested ? Davo ---------------------------------------------------------------------- PierceNg · 2020-08-26 00:29 Try Post instead of FormPost. The source code shows that FormPost hardcodes the content type. ---------------------------------------------------------------------- dbannon · 2020-08-26 01:27 Thanks PierceNg, I was just coming back to report exactly that ! Client.PostForm was overwriting json with x-www-form-urlencoded, perhaps because of 'form' ? Anyway, doing 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 client.RequestBody := TRawByteStringStream.Create(Params); client.Post(TheURL, Response); bypassed the overwriting and all worked as expected. Might add that to the wiki .....  (PierceNG, sounds like you use this a bit, you might like to check what I write for me ?) Davo ---------------------------------------------------------------------- dbannon · 2020-08-26 01:41 Done https://wiki.lazarus.freepascal.org/fphttpclient#Posting_JSON Davo ---------------------------------------------------------------------- PierceNg · 2020-08-26 04:57 Quote from: dbannon on August 26, 2020, 03:27:14 am(PierceNG, sounds like you use this a bit, you might like to check what I write for me ?) Sure. I've created a wiki account recently and plan/hope to write something. Just read what you posted. It's good.