Page 1 of 1

Trouble with post request

Posted: Tue Jun 07, 2022 1:23 pm
by lgiammattei
Hi everybody, hope someone could help me understando qhat i am doing wrong.
I am using httpapi to perform a POST request, the expected result is a JSON file that i want to save in the IFS for later use. The JSON contains a JWT token but that is not important now.
I tried the request using SOAP UI and it works smooth, trying to replicate the request via httpapi a get an error.

Here is the raw request as it appears on the SOAPUI client window
POST https://xxxxxxx.xxxx.xx/identityserver/ ... &scope=ccc HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 82
Host: xxxxxxx.xxxx.xx
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

here is the raw response from SOAP UI client
HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, max-age=0
Pragma: no-cache
Content-Type: application/json; charset=UTF-8
Server: Microsoft-IIS/8.5
X-Powered-By: ARR/3.0
Date: Tue, 07 Jun 2022 12:31:35 GMT
Content-Length: 803

{"access_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6IjM5ODUzQzhBOEI3RjU2QkJBQ0IxMzg2QjBDOTdBNkJFNzE0OENFRkUiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJPWVU4aW90X1ZydXNzVGhyREplbXZuRkl6djQifQ.eyJuYmYiOjE2NTQ2MDUwOTgsImV4cCI6MTY1NDYwODY5OCwiaXNzIjoiaHR0cHM6Ly9zZXJ2aXppLmZpZ2MuaXQvaWRlbnRpdHlzZXJ2ZXIiLCJhdWQiOlsiaHR0cHM6Ly9zZXJ2aXppLmZpZ2MuaXQvaWRlbnRpdHlzZXJ2ZXIvcmVzb3VyY2VzIiwiTE5EIl0sImNsaWVudF9pZCI6IkxuZENsaWVudCIsInNjb3BlIjpbIkxORCJdfQ.YyNbsNrBsnmTJABN2YXzwzjFEm3o6zXcLe_X6Ha9H95WkSGkOJNIt0Wj0G9WfNJtKJXjb24ZXW_Ml2-2yy8sYS3zeDfOyjJHFvSZOSHr2sy2ePvBd68oqG62ELAnYBW90XAk69sBuQsUoJQv6V_TzRSApggy5TDV-NAAt4FuNmqriTv6ckFhbxXzlDnHhakphmBf6UGlVOQF0QB5-pMHJ3buUVlIPhk9rHg583d2EAypmZfyE5LuNIGPDhe8X8zAELSYFEWEALYyWOJiFFvOHWs-BIJFKPl9F18QN3ItJAA1L1SJeq2ytHI2lIKgKXol04O-m7ayL7pOuI_aEavHnQ","expires_in":3600,"token_type":"Bearer"}

here is my rpg snippet code

Code: Select all

       http_debug(*ON: debugLog + 'httpapi_testLog.log');

       http_setCCSIDs( 1208: 0 );

       URLTOJWT='https://xxxxxxx.xxxx.xx/identityserver/connect/token';
       postData='?client_id=' + %Trim(TClientId) +
                '&client_secret=' + %Trim(TClientSecret) +
                '&grant_type=' + %Trim(TGrantType) +
                '&scope=' + %Trim(TScope);


       IFS=debugLog + 'httpapi_testJSON.json';

       rc = http_post(URLTOJWT
                      : %addr(postData) + 2
                      : %len(postData)
                      : IFS
                      : HTTP_TIMEOUT
                      : HTTP_USERAGENT
                      : 'application/x-www-form-urlencoded' );
Following is a snipet from the testlog file
Protocol Used: TLS Version 1.2
http_persist_post(): entered
http_persist_req(POST) entered.
http_long_ParseURL(): entered
http_long_ParseURL(): entered
do_oper(POST): entered
There are 0 cookies in the cache
POST /identityserver/connect/token HTTP/1.1
Host: xxxxxxx.xxxx.xx
User-Agent: http-api/1.45
Content-Type: application/x-www-form-urlencoded
Content-Length: 83


senddoc(): entered
?client_id=aaaaaaa&client_secret=bbbbbb&grant_type=client_credentials&scope=ccc
recvresp(): entered
HTTP/1.1 400 Bad Request
Cache-Control: no-store, no-cache, max-age=0
Pragma: no-cache
Content-Type: application/json; charset=UTF-8
Server: Microsoft-IIS/8.5
X-Powered-By: ARR/3.0
Date: Tue, 07 Jun 2022 13:07:15 GMT
Content-Length: 26


SetError() £13: HTTP/1.1 400 Bad Request
recvresp(): end with 400
recvdoc parms: identity 26
header_load_cookies() entered
recvdoc(): entered
SetError() £0:
recvdoc(): Receiving 26 bytes.
{"error":"invalid_client"}
SetError() £13: HTTP/1.1 400 Bad Request
http_close(): entered

and in the json file i find only {"error":"invalid_client"}

Can any kind soul out there point me to an error or mistake i am doing?

TIA

Re: Trouble with post request

Posted: Tue Jun 07, 2022 1:26 pm
by lgiammattei
i forgot to write that i am using
HTTPAPI Ver 1.45 released 2021-09-20
NTLM Ver 1.4.0 released 2014-12-22
OS/400 Ver V7R3M0

Re: Trouble with post request

Posted: Tue Jun 07, 2022 3:22 pm
by Scott Klement
Are you certain that you should be sending your data in the URL? That sort of defeats the point of using POST, doesn't it?

Also, you don't appear to be calling the url-encoding routines. Is the data already encoded?

Re: Trouble with post request

Posted: Tue Jun 07, 2022 8:47 pm
by lgiammattei
Scott thanks for your attention,
Are you certain that you should be sending your data in the URL? That sort of defeats the point of using POST, doesn't it?
i know, but in SOAP UI it works
Also, you don't appear to be calling the url-encoding routines. Is the data already encoded?
that is something i forgot, now i am out of office but tomorrow morning will be my first try.

Thx again

Re: Trouble with post request

Posted: Wed Jun 08, 2022 10:35 am
by lgiammattei
Solved. I had made a mistake in the postData string variable for the request

Code: Select all

postData='?client_id=' + %Trim(TClientId) +
                '&client_secret=' + %Trim(TClientSecret) +
                '&grant_type=' + %Trim(TGrantType) +
                '&scope=' + %Trim(TScope);
deleting the question mark at the begin of the string (i copied and pasted the string from the SOAP Ui screen) made everything works fine.