API call with Headers
API call with Headers
Hello, attached I have posted 2 screenshots. One shows how I make a call from Postman that works, and the other screenshot shows my RPGLE code that gives me an error "HTTP/1.1 500 Internal Server Error". What am I doing wrong?
- Attachments
-
- Screenshot_POSTMAN.png (17.93 KiB) Viewed 31273 times
-
- Screenshot RDI.png (14.73 KiB) Viewed 31273 times
Re: API call with Headers
Why are you building the headers like that? HTTPAPI builds the headers for you so the resulting data you are sending to the server will be a mess.
The data you are sending is already built-into the URL you are passing. You didn't duplicate the request data in the Postman example did you? I doubt it. Looks to me like your whole request field content is spurious and that you do not need to pass a request body at all.
The result is that the server is just basically barfing on the request.
The data you are sending is already built-into the URL you are passing. You didn't duplicate the request data in the Postman example did you? I doubt it. Looks to me like your whole request field content is spurious and that you do not need to pass a request body at all.
The result is that the server is just basically barfing on the request.
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: API call with Headers
So you are building an XML document formatted the way IBM required it for their old "SYSTOOLS" (Java-based) SQL HTTP functions. Then you are passing it as the request body to HTTPAPI.
Request body is not headers -- so this isn't going to do anything useful... it's just going to send the XML document to the API.
Plus, I'm very confused as to why you think the XML document from a completely different product would work here?
Request body is not headers -- so this isn't going to do anything useful... it's just going to send the XML document to the API.
Plus, I'm very confused as to why you think the XML document from a completely different product would work here?
Re: API call with Headers
Sorry, I am new to this world and still have a lot to learn. So how should I structure a request of this type?
Re: API call with Headers
I tried to do it this way, but the error is always the same. How can I pass 'Authorization-scope = TSEV12' to it, which in Postman is included among the headers? I saw that by removing 'Authorization-scope = TSEV12' from Postman, I still get the same error, so the issue is probably that...
- Attachments
-
- Screenshot hhh.png (18.32 KiB) Viewed 31210 times
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: API call with Headers
in HTTPAPI, headers that the tool doesn't know about are added via an "exit procedure" (or "xproc" for short.) This is a routine that you write, but HTTPAPI calls during network transmission. It will call the routine to get a string of additional headers to add to the request. It is up to you to format these headers in compliance with the HTTP specification.
To use this, you should set your xproc prior to calling http_string (or whichever routine you prefer)
Now you call http_string() or http_req() or whatever you prefer. During the HTTP request, it will call the "AddHeaders" routine (or whichever routine you specified with %paddr, above) to get the headers. The routine would look like this:
All HTTP requests you send after this will call this procedure again, so the headers will be added each time. To tell HTTPAPI to stop calling this procedure, you should code:
To use this, you should set your xproc prior to calling http_string (or whichever routine you prefer)
Code: Select all
// Set up a subprocedure to be called to add additional
// headers to the request.
http_xproc( HTTP_POINT_ADDL_HEADER: %paddr(AddHeaders));
Code: Select all
dcl-proc AddHeaders;
dcl-pi *n;
toBeAdded varchar(32767);
end-pi;
toBeAdded = 'Authorization-Scope: TSEV12' + x'0d25';
// NOTE: If you wanted to add multiple headers, they would
// all be in the same string such as:
//
// toBeAdded = 'x-example-header: example value' + x'0d25'
// + 'HeaderTwo: data data data' + x'0d25'
// + 'Identity: root' + x'0d25';
end-proc;
Code: Select all
http_xproc(HTTP_POINT_ADDL_HEADER: *NULL);
Re: API call with Headers
Perfect! It works! Thank you so much for your availability 

Re: API call with Headers
Now I have another error! The response I get from the GET is this:
*********************************************************************************
{"aliqivavent": null,
"annotazioni": null,
"codice": "22",
"codiceagr": null,
"codiceOss": null,
"codPlafond": 0.0,
"descrizione": "Iva Impon. 22%",
"flgAgri": 0.0,
"flgAllclifor": 0.0,
"flgAssport398": 0.0,
"flgAutoue": 0.0,
"flgCorrVent": 0.0,
"flgEscludiblacklist": 0.0,
"flgImpostadibollo": 0,
"flgIndet": 0.0,
"flgIvaedit": 0.0,
"flgMonofasersm": 0.0,
"flgMossgest": 0,
"flgMossrid": 0,
"flgNotvar": 0.0,
"flgSospimp": 0.0,
"idprov": 25.0,
"impostamonofasersm": null,
"indNatassoswCg2n": null,
"indNatura": null,
"indStaper": 0.0,
"indtipopart": 0.0,
"mosscodCg07": null,
"mossperc": null,
"note": null,
"percforf": null,
"percindet": null,
"perciva": 22.00,
"percmonofasersm": null,
"rowVersion": "AAAAAAAADyc=",
"tipologia": 1.0,
"verslynfa": "2018.00.04",
"idExtendedAttributeEntity": 61,
"idExtendedAttributeSubEntity": 63,
"nationCO": null,
"natureAssCO": null,
"natureEsCO": null,
"vatCodeCOAgr": null,
"vatCodeCOOss": null,
"extensionData": [],
" ": {}
}
*********************************************************************************
I built the DS like this:
*********************************************************************************
dcl-ds DsJson qualified;
num_aliqivavent int(10);
annotazioni varchar(10);
codice varchar(2);
codiceagr varchar(10);
codiceOss varchar(10);
num_codPlafond int(10);
descrizione varchar(20);
num_flgAgri packed(10:2);
num_flgAllclifor packed(10:2);
num_flgAssport398 packed(10:2);
num_flgAutoue packed(10:2);
num_flgCorrVent packed(10:2);
num_flgEscludiblacklist packed(10:2);
num_flgImpostadibollo packed(10:2);
num_flgIndet packed(10:2);
num_flgIvaedit packed(10:2);
num_flgMonofasersm packed(10:2);
num_flgMossgest int(10);
num_flgMossrid int(10);
num_flgNotvar packed(10:2);
num_flgSospimp packed(10:2);
num_idprov packed(10:2);
impostamonofasersm varchar(10);
num_indNatassoswCg2n int(10);
num_indNatura int(10);
num_indStaper packed(10:2);
num_indtipopart packed(10:2);
mosscodCg07 varchar(10);
mossperc varchar(10);
note varchar(10);
percforf varchar(10);
percindet varchar(10);
num_perciva packed(10:2);
percmonofasersm varchar(10);
rowVersion varchar(12);
num_tipologia packed(10:2);
verslynfa varchar(10);
num_idExtendedAttributeEntity int(10);
num_idExtendedAttributeSubEntity int(10);
nationCO varchar(10);
natureAssCO varchar(10);
natureEsCO varchar(10);
vatCodeCOAgr varchar(10);
vatCodeCOOss varchar(10);
dcl-ds extensionData dim(999);
key varchar(100);
value varchar(100);
END-DS;
additionalData varchar(100);
end-ds;
*********************************************************************************
and I receive the error message attached. How should I structure the DS?
*********************************************************************************
{"aliqivavent": null,
"annotazioni": null,
"codice": "22",
"codiceagr": null,
"codiceOss": null,
"codPlafond": 0.0,
"descrizione": "Iva Impon. 22%",
"flgAgri": 0.0,
"flgAllclifor": 0.0,
"flgAssport398": 0.0,
"flgAutoue": 0.0,
"flgCorrVent": 0.0,
"flgEscludiblacklist": 0.0,
"flgImpostadibollo": 0,
"flgIndet": 0.0,
"flgIvaedit": 0.0,
"flgMonofasersm": 0.0,
"flgMossgest": 0,
"flgMossrid": 0,
"flgNotvar": 0.0,
"flgSospimp": 0.0,
"idprov": 25.0,
"impostamonofasersm": null,
"indNatassoswCg2n": null,
"indNatura": null,
"indStaper": 0.0,
"indtipopart": 0.0,
"mosscodCg07": null,
"mossperc": null,
"note": null,
"percforf": null,
"percindet": null,
"perciva": 22.00,
"percmonofasersm": null,
"rowVersion": "AAAAAAAADyc=",
"tipologia": 1.0,
"verslynfa": "2018.00.04",
"idExtendedAttributeEntity": 61,
"idExtendedAttributeSubEntity": 63,
"nationCO": null,
"natureAssCO": null,
"natureEsCO": null,
"vatCodeCOAgr": null,
"vatCodeCOOss": null,
"extensionData": [],
" ": {}
}
*********************************************************************************
I built the DS like this:
*********************************************************************************
dcl-ds DsJson qualified;
num_aliqivavent int(10);
annotazioni varchar(10);
codice varchar(2);
codiceagr varchar(10);
codiceOss varchar(10);
num_codPlafond int(10);
descrizione varchar(20);
num_flgAgri packed(10:2);
num_flgAllclifor packed(10:2);
num_flgAssport398 packed(10:2);
num_flgAutoue packed(10:2);
num_flgCorrVent packed(10:2);
num_flgEscludiblacklist packed(10:2);
num_flgImpostadibollo packed(10:2);
num_flgIndet packed(10:2);
num_flgIvaedit packed(10:2);
num_flgMonofasersm packed(10:2);
num_flgMossgest int(10);
num_flgMossrid int(10);
num_flgNotvar packed(10:2);
num_flgSospimp packed(10:2);
num_idprov packed(10:2);
impostamonofasersm varchar(10);
num_indNatassoswCg2n int(10);
num_indNatura int(10);
num_indStaper packed(10:2);
num_indtipopart packed(10:2);
mosscodCg07 varchar(10);
mossperc varchar(10);
note varchar(10);
percforf varchar(10);
percindet varchar(10);
num_perciva packed(10:2);
percmonofasersm varchar(10);
rowVersion varchar(12);
num_tipologia packed(10:2);
verslynfa varchar(10);
num_idExtendedAttributeEntity int(10);
num_idExtendedAttributeSubEntity int(10);
nationCO varchar(10);
natureAssCO varchar(10);
natureEsCO varchar(10);
vatCodeCOAgr varchar(10);
vatCodeCOOss varchar(10);
dcl-ds extensionData dim(999);
key varchar(100);
value varchar(100);
END-DS;
additionalData varchar(100);
end-ds;
*********************************************************************************
and I receive the error message attached. How should I structure the DS?
- Attachments
-
- Screenshot 2024-07-16 101713.png (13.31 KiB) Viewed 30979 times
Re: API call with Headers
Actually, it seems that the problem is the 'null' fields and I solved like this:
*********
YAJLOPTS='{"value_null":"0"}';
DATA-INTO Dsjson %DATA( response
: 'case=convert +
allowextra=yes +
allowmissing=yes')
%PARSER('YAJLINTO': YAJLOPTS);
*********
But this way the issue remains for char fields. How do I pass the option for both numeric and char fields?
*********
YAJLOPTS='{"value_null":"0"}';
DATA-INTO Dsjson %DATA( response
: 'case=convert +
allowextra=yes +
allowmissing=yes')
%PARSER('YAJLINTO': YAJLOPTS);
*********
But this way the issue remains for char fields. How do I pass the option for both numeric and char fields?
-
- Site Admin
- Posts: 872
- Joined: Sun Jul 04, 2021 5:12 am
Re: API call with Headers
The way DATA-INTO was designed (by IBM) is that the parser doesn't know how the RPG field is defined, so it can't pass a separate value for numbers vs. characters... it can only pass a value, and then let RPG handle it.RINALDI_V wrote: ↑Tue Jul 16, 2024 3:52 pmBut this way the issue remains for char fields. How do I pass the option for both numeric and char fields?Code: Select all
YAJLOPTS='{"value_null":"0"}'; DATA-INTO Dsjson %DATA( response : 'case=convert + allowextra=yes + allowmissing=yes') %PARSER('YAJLINTO': YAJLOPTS);
What you are doing WOULD work for both characters and numbers... they'd all get a zero in them (which works because zero is perfectly valid in a character field.) It seems to me that the problem is that it's awfully hard to know whether 0 is a legitimate value, or whether it means null. (Which is why YAJLINTO doesn't pass 0 by default.)
One thought is to do something like this:
Code: Select all
YAJLOPTS='{"value_null":"-999"}';
Code: Select all
if myds.mycharfield = '-999';
// value was null
endif;
if myds.mynumfield = -999;
// value was null
endif;