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?

6
  • 1
    To find the difference capture the Indy HTTP request with a proxy (Fiddler2) and compare it with the successful request Commented Nov 25, 2015 at 19:51
  • id-WHAT ? make a post using the IdFtp ??? FTP and HTTP are totally different protocols and components! Commented Nov 25, 2015 at 20:09
  • General framework (as mjn suggested, more details) - stackoverflow.com/questions/17546558 Commented Nov 25, 2015 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! Commented Nov 25, 2015 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. Commented Nov 26, 2015 at 5:38

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.