0

I'm trying to make a post using the IdHttp, sending data in json format, except that in every way I tested gives error on the server, the same data has successfully submitted for testing POSTMAN tool

strPost := '{...}'; //string json
xUrlPost := 'url...'; //url to post

TIdHttp configuration

IdHTTPLibSys.Request.ContentType := 'application/json';
IdHTTPLibSys.Request.AcceptCharSet := 'UTF-8';
IdHTTPLibSys.Request.CustomHeaders.Add('Authorization: Bearer xToken');
IdHTTPLibSys.Request.CustomHeaders.Add('ClientInfo: 528533378');

using TStringList

JSonToSend3 := TStringList.Create;
JSonToSend3.Add(strPost);
sHtmlResp := IdHTTPLibSys.Post(xUrlPost, JSonToSend3);

ERROR: HTTP/1.1 500 Internal Server Error

using TStringStream

JsonToSend := TStringStream.Create(strPost, TEncoding.UTF8);
sHtmlResp := IdHTTPLibSys.Post(xUrlPost, JSonToSend);

ERROR: HTTP/1.1 422 Unprocessable Entity

using TMemoryStream

JsonToSend2 := TMemoryStream.Create;
JsonToSend2.Write(strPost[1], Length(strPost) * SizeOf(strPost[1]));
JsonToSend2.Position := 0;

ERROR: HTTP/1.1 500 Internal Server Error

Recalling that the same data posted via POSTMAN (chrome) does not occur error, the json is correct, any ideas?

Jason-X
  • 39
  • 2
  • 11
  • 1
    To find the difference capture the Indy HTTP request with a proxy (Fiddler2) and compare it with the successful request – mjn Nov 25 '15 at 19:51
  • id-WHAT ? `make a post using the IdFtp` ??? FTP and HTTP are totally different protocols and components! – Arioch 'The Nov 25 '15 at 20:09
  • General framework (as mjn suggested, more details) - http://stackoverflow.com/questions/17546558 – Arioch 'The Nov 25 '15 at 20:13
  • Put a component "IdLogDebug" and worked the events "OnReceive" and "OnSend" the "TIdConnectionIntercept" to see what was leaving and returning, using "TStringList" and "TMemoryString" the json not exit properly, since using "TStringStream" the json is posted in full, plus get "HTTP / 1.1 422 Unprocessable Entity" but I get the json data in Buffer (TIdBytes) of "OnReceive" event, any idea in this case? thank you again! – Jason-X Nov 25 '15 at 22:42
  • As mjn suggested, it would be better to troubleshoot this issue using Wireshark or Fiddler, because they will show you the working request from Postman/Chrome so you can compare it to the non-working request from TIdHTTP and see differences between them. Using IdLog... components will not do that for you. – Remy Lebeau Nov 26 '15 at 05:38
  • maybe you could use SuperObject or lkJSON or mORMot or other proven library to generate correct JSON payload – Arioch 'The Nov 26 '15 at 08:58

1 Answers1

1

Posting a TStringList sends the data in application/x-www-form-urlencoded format, which is not what the server is expecting.

Using a UTF-8 encoded TStringStream will work just fine. The 422 reply does not mean a problem occurred with the post itself, it means there was a problem with the JSON data and the server could not process it. You likely have a syntax error in the JSON.

Using a TMemoryStream will also work fine. However, you are writing the JSON string as UTF-16 instead of encoding it to UTF-8. You can use Indy's WriteStringToStream() function for that, eg: WriteStringToStream(JsonToSend2, strPost, IndyTextEncoding_UTF8);

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The json is correct, so that sending via POSTMAN get the OK and return the data. with IdHttp, the error occurs 442, but the data that I should receive from the Post answer, I can get at the "OnReceive" the "IdConnectionIntercept", therefore the server could read the json somehow. I made the change to `WriteStringToStream (JsonToSend2, strPost, IndyTextEncoding_UTF8);` and still the same error – Jason-X Nov 27 '15 at 01:39