FedEx Address validation API

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
spala
Posts: 3
Joined: Fri Jul 12, 2024 6:52 pm

FedEx Address validation API

Post by spala »

FedEx APIs support the OAuth 2.0 (bearer token) authentication method to authorize application API requests. The first step is to receive the access token. This request works just fine when tested using Postman.
But, the iSeries request comes back with timeout. Not sure whats missing.

Any help is greatly appreciated.

Postman request
----------------------

POST /oauth/token HTTP/1.1
User-Agent: PostmanRuntime/7.40.0
Accept: */*
Cache-Control: no-cache
Postman-Token: f9ff1c21-a5e8-4494-880c-bf85392fb758
Host: apis-sandbox.fedex.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 121

grant_type=client_credentials&client_id=<client_id provided by FedEx>&client_secret=<client_secret provided by FedEx>

HTTP/1.1 200 OK
X-API-Mode: Sandbox
Pragma: no-cache
Cache-Control: no-store
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Content-Length: 1048
Server: Layer7-API-Gateway
Date: Fri, 12 Jul 2024 19:47:00 GMT
Connection: keep-alive
Vary: Accept-Encoding
Server-Timing: cdn-cache; desc=MISS
Server-Timing: edge; dur=7
Server-Timing: origin; dur=374
Server-Timing: ak_p; desc="1720813619754_389294938_290214488_38145_7843_309_65_-";dur=1

{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJDWFMtVFAiXSwiUGF5bG9hZCI6eyJjbGllbnRJZGVudGl0eSI6eyJjbGllbnRLZXkiOiJsN2IxMD
FmMjYyM2M5MzQ3MjViZjQwOTczYjI1MGNhMmI3In0sImF1dGhlbnRpY2F0aW9uUmVhbG0iOiJDTUFDIiwiYWRkaXRpb25hbElkZW50aXR5Ijp7InRpbWVTdGFtcCI6IjEyLUp1bC0yMDI0ID
E0OjQ3OjAwIEVTVCIsImdyYW50X3R5cGUiOiJjbGllbnRfY3JlZGVudGlhbHMiLCJhcGltb2RlIjoiU2FuZGJveCIsImN4c0lzcyI6Imh0dHBzOi8vY3hzYXV0aHNlcnZlci1zdGFnaW5nLm
FwcC5wYWFzLmZlZGV4LmNvbS90b2tlbi9vYXV0aDIifSwicGVyc29uYVR5cGUiOiJEaXJlY3RJbnRlZ3JhdG9yX0IyQiJ9LCJleHAiOjE3MjA4MTcyMjAsImp0aSI6IjhjYjE1NzIwLTYzY2
MtNDUwNi05OWRmLThhODdlNTAyMDdkYyJ9.mg9zaRpIhy_AW3YFgCxkIVFWTOFoLkBLakDfxo_I1lKAjIyyM0nnuppDvhyB_opquvUNOWvxY7V6eCc3tiMNxR1nSkxMCtmgABtN1IVxqS4ef
5gSI14KqXoK6I-cxD1rg1M0O7p_n-uTdl9WZkfuHadN70RSNkBkwrWw23zmzt3biOXHhvTkTdwE-zdvbVyHqjb84keBSARrdxib5NnJ-lwsIHxOZ184shyjWq9YOyOnkTVnNJAmGX_VBnJfn
pZ1vH0Fkzxz1zB-QgD-wNCxKy2htFjaLQeB7IQ0m88EE2vL2i4ivhXBixB6OduBEjGVdHZ4EH3mClLJJPZPTE41zc5yDW12xa089enoQ7cbTKmMasgbpmgZGSS_aNi_bLKK6v36H4ANmf4px
qPWNntsIXi8_d4osTUsrXhhtGXozJmTXAsYL_8DpbJFjG4Hj-SyEd9MFt2u7qtO-Mrdzb7Y9ZgR88A482DpgrGcq2d5DSs7LVZfX5gVfqtJ_Kc3RO8RMhohUgj9_o9C2K2vfSD-p51qP-08u
VfaxIm-PSQrFxrPHE7x5U7y-n9WkWVGeoKiojWrkC-vZ20Enom-wCwSsx3NLvXaz36yi9lhUtOh9ykFJByZA2znNVakLVsPfjCPQIN5fXbcqsbt9GEt3GtclDqE-Leg7NMhN7RxkMnqLGE",
"token_type":"bearer",
"expires_in":3599,
"scope":"CXS-TP"}


iSeries request - debug/trace
----------------------------------------

************Beginning of data**************
HTTPAPI Ver 1.45 released 2021-09-20
NTLM Ver 1.4.0 released 2014-12-22
OS/400 Ver V7R4M0

http_setauth(): entered
http_persist_open(): entered
http_long_ParseURL(): entered
DNS resolver retrans: 2
DNS resolver retry : 2
DNS resolver options: x'00000136'
DNS default domain: <defalut domain>
DNS server found: 10.10.10.10
DNS server found: 10.10.10.12
DNS server found: 10.9.10.10
Nagle's algorithm (TCP_NODELAY) disabled.
SNI hostname set to: apis-sandbox.fedex.com
-------------------------------------------------------------------------------------
Dump of server-side certificate information:
-------------------------------------------------------------------------------------
Cert Validation Code = 0
-----BEGIN CERTIFICATE-----
<<<< certificate details>>>>
-----END CERTIFICATE-----
Serial Number: 00:EF:C4:CE:E3:EC:66:DB:B5:1D:1B:45:05:91:02:23:C4
Common Name: apis.fedex.com
Country: US
State/Province: Tennessee
Org Unit: FedEx Corporation
Issuer CN: Sectigo RSA Organization Validation Secure Server CA
Issuer Country: GB
Issuer State/Province: Greater Manchester
Issuer Locality: Salford
Issuer Org: Sectigo Limited
Version: 3
not before: 20240110180000
Unknown Field: 18:00:00 10-01-2024
not after: 20250209175959
Unknown Field: 17:59:59 09-02-2025
pub key alg: 1.2.840.113549.1.1.1
signature algorithm: 1.2.840.113549.1.1.11
Unknown Field: 0382010F003082010A02820101008FACBB825E6250DDA7AF73ABC2682AD953BDA170651EFCD7F895859ABE2A48509E6D760D1CD26A8A645514077
Unknown Field: 2048
Unknown Field: 4508EED6E6ECAD4A50FF469BC1733E3A
Unknown Field: 1.2.840.113549.2.5
Unknown Field: 95791B746A2E3F6B92D40BA5ADA5B466EFE67DDE
Unknown Field: 09B321BA7B54716F3C760510B04D8B7C4E560AFEB68232574983517F779C09B7
Unknown Field: 5
Unknown Field: secure-api.fedex.com
Unknown Field: apis-sandbox.fedex.com
Unknown Field: api.fedex.com
Unknown Field: apis.fedex.com
Unknown Field: 0
Unknown Field: 1.3.6.1.5.5.7.3.2
Unknown Field: 1.3.6.1.5.5.7.3.1
Unknown Field: 2.23.140.1.2.2
Unknown Field: 1.3.6.1.4.1.6449.1.2.1.3.4
Unknown Field: http://crl.sectigo.com/SectigoRSAOrgani ... rverCA.crl
Unknown Field: http://ocsp.sectigo.com
Unknown Field: http://crt.sectigo.com/SectigoRSAOrgani ... rverCA.crt

Protocol Used: TLS Version 1.3
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 /oauth/token?grant_type=client_credentials&client_id=<client_id provided by FedEx>&client_secret=<client_secret provided by FedEx>
Host: apis-sandbox.fedex.com
User-Agent: http-api/1.45
Content-Length:121
recvresp(): entered
SetError() #43: CommSSL_read: timeout!
recvresp(): end with timeout
http_close(): entered
************End of Data********************
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: FedEx Address validation API

Post by Scott Klement »

It says "content-length: 121", but there's no sign here of you sending anything... the server is waiting for the data to be sent, that is why it is timing out.

What are you passing to HTTPAPI? Show your code.
spala
Posts: 3
Joined: Fri Jul 12, 2024 6:52 pm

Re: FedEx Address validation API

Post by spala »

Thanks for the quick response, Scott.

Passing client id and client secret should give an access token as a first step. There is no separate content for this step.
When I sent the request without the 'content-length', received an error 'content-length' is missing.

Here is the code.
--------------------------

X_API_Key & X_Client_Secret are retrieved from a table.

Exec SQL
VALUES SYSTOOLS.URLENCODE(:X_API_Key,'UTF-8')
INTO :X_API_Key_2;

Exec SQL
VALUES SYSTOOLS.URLENCODE(:X_Client_Secret,'UTF-8')
INTO :X_Client_Secret_2;

X = %SCAN( '++++++++++' : X_API_Key_2 : 1 );
X_API_Key_2 = %subst( X_API_Key_2 : 1 : (X - 1));

X = %SCAN( '++++++++++' : X_Client_Secret_2 : 1 );
X_Client_Secret_2 = %subst( X_Client_Secret_2 : 1 : (X - 1));

RequestURL = %trim(RequestURL) +
'&client_id=' + %trim(X_API_Key_2) +
'&client_secret=' + %trim(X_Client_Secret_2) ;

http_debug(*ON: '/home/FedEx_API_Log.txt');

http_xproc( HTTP_POINT_ADDL_HEADER : %paddr(addSpecialHeaders_1) );

If http_req(
'POST':
%trim(RequestURL):
*OMIT:
ResponseJSON:
*OMIT:
*OMIT:
'application/x-www-form-urlencoded') <> 1;

http_error(http_err) ;

Endif;

// -----------------------------------------------------------------------
Dcl-Proc addSpecialHeaders_1;
Dcl-Pi *N;
headersToAdd Varchar(32767);
End-Pi;

Dcl-c CRLF x'0d25';

headersToAdd = 'Content-Length:121' + CRLF ;

End-Proc;
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: FedEx Address validation API

Post by Scott Klement »

You shouldn't ever be manually adding the content-length. That is the reason this is timing out for you. You are telling it "I'm going to send you 121 bytes" (which you have hard-coded, so it must ALWAYS be 121 exactly) but then for the parameter where you specify the data to send, you are passing *OMIT, so there's no data at all.

Even if you sent the data correctly, it wouldn't work because now HTTPAPI would add the length of the data, PLUS you're also adding it. That makes no sense. You should never supply the length of the data -- let HTTPAPI do it! That's it's job!

The reason you were getting an error about the content-length being missing is because you're passing *OMIT for the data to send, and apparently this API requires data.

I strongly suspect that the stuff you're encoding (no clue why you are using the SYSTOOLS routines, or why you are encoding blanks and then using %SCAN to remove the resulting pluses) belongs in the body of the request, NOT on the end of the URL. If I'm right, the code would look more like this:

Code: Select all

        dcl-s sendData varchar(500);

         sendData = 'client_id='     + HTTP_urlEncode(%trim(X_API_Key)) +
                    '&client_secret=' + HTTP_urlEncode(%trim(X_Client_Secret_2)) ;

         http_debug(*ON: '/home/FedEx_API_Log.txt');

         If  http_req(
             'POST':
             %trim(RequestURL):
             *OMIT:
             ResponseJSON:
             *OMIT:
             sendData:
             'application/x-www-form-urlencoded')  <>  1;

           http_error(http_err) ;

         Endif;
Notice how much simpler this is? The data to send will look like "client_id=xxxx&client_secret=yyyyy" (the & separates one variable from the next, and only belongs BETWEEN them, not at the start) I'm assuming that data in your example happens to be 121 bytes (after it has been translated to UTF-8 or whatever) so simply providing it as the data to send will cause HTTPAPI to automatically tell it that the content-length is 121.
spala
Posts: 3
Joined: Fri Jul 12, 2024 6:52 pm

Re: FedEx Address validation API

Post by spala »

Thanks a lot, Scott.
Cleaned up version of the code successfully retrieved the access token.
Post Reply