![]() |
Post in HTTPs - Printable Version +- Atozed Forums (https://www.atozed.com/forums) +-- Forum: Indy (https://www.atozed.com/forums/forum-8.html) +--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html) +--- Thread: Post in HTTPs (/thread-771.html) |
Post in HTTPs - staff@ergosoft.it - 10-25-2018 Hi, I have a problem with a call in https with method POST. from the documentation... ... POST /auth/signin the url is https://demoauth.fatturazioneelettronica.aruba.it this the istruction "... POST /auth/signin?grant_type=password&username=Utente&password=Password HTTP/1.1 Content-Type: application/x-www-form-urlencoded;charset=UTF-8 Content-Length: 56 Host: localhost:8080 ..." when try to executo the code i receive this error "...Error HTTP/1.1 400 Bad request' ----------------------------------------------------------- this my Code: Delphi 10.2.2 Tokyo ----------------------------------------------------------- var lHTTP: TIdHTTP; lParamList: TStringList; HttpAutenticazione : string; begin HttpAutenticazione := 'https://demoauth.fatturazioneelettronica.aruba.it'; lParamList := TStringList.Create; lParamList.Add('grant_type=password'); lParamList.Add('username=demo'); lParamList.Add('password=demo'); lHTTP := TIdHTTP.Create; lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP); lHTTP.HandleRedirects := True; lHttp.Request.ContentType := 'application/x-www-form-urlencoded;charset=UTF-8'; lHttp.Request.ContentLength := 56; lHttp.Request.Host := 'localhost:8080'; try Result := lHTTP.Post(HttpAutenticazione+'/auth/signin', lParamList); finally lHTTP.Free; lParamList.Free; end; RE: Post in HTTPs - rlebeau - 10-25-2018 (10-25-2018, 07:33 AM)staff@ergosoft.it Wrote: from the documentation... Where is the documentation located exactly? I can't find it. However, from just what you have quoted above, the documented request looks wrong. The login post data is being put in the URL query string (which is fine), but the request includes a Content-Length header that is not 0, implying that post data is also in the request body as well, which may or may not be fine depending on what that post data actually looks like, which you did not show from the documentation. (10-25-2018, 07:33 AM)staff@ergosoft.it Wrote: when try to executo the code i receive this error Using the exact code you have shown, I do not get an "HTTP/1.1 400 Bad request" response. I get an "HTTP/1.1 400" (no "Bad Request") response. But more importantly, the response body contains the following JSON document: Code: {"error":"invalid_grant","error_description":"The user name or password is incorrect."} You can access that JSON by catching the raised EIdHTTPProtocolException and read its ErrorMessage property: Code: try Or, you can enable TIdHTTP's hoNoProtocolErrorException and hoWantProtocolErrorContent flags, then Post() will not raise the EIdHTTPProtocolException and the returned string will be the JSON: Code: lHTTP.HTTPOptions := lHTTP.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent]; That being said, there are some issues with your code: Code: // Post(TStrings) will set the ContentType for you... Just remove those 3 lines of code, you don't need them. Also, your try..finally should be tweaked a little. The below code produces the same "The user name or password is incorrect" response: Code: var RE: Post in HTTPs - staff@ergosoft.it - 10-26-2018 THANKS ! now is ok.... |