Atozed Forums

Full Version: Post JSON with IDHTTP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to post a JSON to web rest service but always with message HTTP1.1/BAD request.
The page in question is test page: https://dev.slimpay.com/hapi/browser#create-payins and the json is
Code:
POST Follow(https://api.slimpay.net/alps#create-payins) HTTP/1.1
Accept: application/hal+json; profile="https://api.slimpay.net/alps/v1"
Authorization: Bearer token
Content-Type: application/json

{
   "creditor": {
       "reference": "democreditor"
   },
   "subscriber": {
       "reference": "subscriber5bea84af2e9e3"
   }
}

The first two steps on the test page OK with IDHTTP and SSL but this test step i am aunable to proceed, my code is the following:

Code:
htpBrwsr.Request.Accept := 'application/hal+json; profile="https://api.slimpay.net/alps/v1"';
htpBrwsr.Request.CustomHeaders.Add('Authorization: Bearer ' + sToken); // token got correctly from previous step
htpBrwsr.Request.ContentType := 'application/json';
// htpBrwsr.Request.CharSet := 'UTF-8';        
ms := TMemoryStream.Create;
ss := TStringStream.Create('{"creditor": {"reference": "democreditor"},"subscriber": {"reference": "subscriber5bea84af2e9e3"}}');
ss.Position := 0;
htpBrwsr.Post('https://api.preprod.slimpay.com/payments/in', ss, ms); // error HTTP1.1/400T


htpBrwsr is an TIDHttp related to a TIdSSLIOHandlerSocketOpenSSL, a similar TIDHttp functions well in the first two steps sited on the test page and I obtain the token with no problems.

any help will be aprecieted..
(11-13-2018, 10:35 AM)mo_ayad Wrote: [ -> ]I am trying to post a JSON to web rest service but always with message HTTP1.1/BAD request.

Is there any payload data in the response body to indicate what the server considered to be bad? By default, TIdHTTP raises an EIdHTTPProtocolException when it receives an HTTP error from the server, where the response payload will be in the exception's ErrorMessage property.

If you enable the hoNoProtocolErrorException and hoWantProtocolErrorContent flags in the TIdHTTP.HTTPOptions property, the EIdHTTPProtocolException will not be raised (so use the TIdHTTP.Response.ResponseCode property to know if the request was successful or not), and the response payload will be written to your TMemoryStream whether an error occurred or not.

(11-13-2018, 10:35 AM)mo_ayad Wrote: [ -> ]The page in question is test page: https://dev.slimpay.com/hapi/browser#create-payins

That page says to POST to 'https://api.slimpay.net/alps#create-payins', so why are you posting to a different URL ('https://api.preprod.slimpay.com/payments/in')?

(11-13-2018, 10:35 AM)mo_ayad Wrote: [ -> ]
Code:
htpBrwsr.Request.CustomHeaders.Add('Authorization: Bearer ' + sToken); // token got correctly from previous step

Be sure to set the TIdHTTP.Request.BasicAuthentication property to False.

Other than that, I don't see anything else wrong with your code. If the server is still having problems with your HTTP request, please show the actual request that TIdHTTP is sending. Since you are using SSL/TLS, you can get that data by assigning one of the TIdLog... components to the TIdHTTP.Intercept property, which will log the request before it is encrypted, and log the response after it has been decrypted.