I need to call an external API to add a product on another system. For this call I need to attach a security token as an Authoization header to the POST HTTP Request (together with another Header Request information like Content-Type ...).

My code looks like this:

create or replace function AddProductAPI ("JsonLoad" long varchar)

returns long varchar

url 'http://localhost/api/v1/product'

type 'http:post:application/json'

Header 'Content-Type: application/json';-- line5


create or replace procedure AddProduct()

begin

call AddProductAPI ('{"Product_Name": "Mouse","ProductID": "123"}');--*line9

end;

In Web Service I can make use of the function HTTP_HEADER in order to fetch the Header information being passed to me as a Web service/server, but how I can set such header information when I am consuming an external services?

I tried to do it on line5 above! (something like):

Header 'Content-Type: application/json, Authorization' + select @MyToken; -- Syntax Error

When I try to do this using SA_SET_HTTP_HEADER(), on the line before line9, then there is no any syntax error, but also no any effect (it does nothing)!

Any ideas please!

asked 03 Apr '20, 08:11

Baron's gravatar image

Baron
2.1k134146175
accept rate: 46%

edited 03 Apr '20, 09:50

Volker%20Barth's gravatar image

Volker Barth
40.1k361549819


The system procedure sa_set_http_header() is meant for web services, not for web client functions so using the HEADER clause within the CREATE FUNCTION statement is the right appraoch AFAIK. But you cannot add a query result there, instead you can use the substitution parameters (i.e. the "!param" syntax) to provide variable contents to your web client function's definition.

Here's a sample from the docs - I guess it's quite easy to use that as a starting point:

CREATE PROCEDURE test(uid CHAR(128), pwd CHAR(128), headers LONG VARCHAR)
    URL 'http://!uid:!pwd@localhost/myservice'
    HEADER '!headers';
permanent link

answered 03 Apr '20, 09:49

Volker%20Barth's gravatar image

Volker Barth
40.1k361549819
accept rate: 34%

Thank you very much.

It was very helpful.

I have another question: How can I issue a PATCH Request? In the documentation is mentioned that there are only 5 types available for http-type-spec-string (so, only GET, POST, PUT, DELETE, HEAD).

If I use this type (PATCH), then I get an error message Invalid Procedure subtype 'PATCH' for type 'HTTP'

(03 Apr '20, 17:44) Baron
Replies hidden
1

Well, v17 also lists the OPTIONS method but I guess PATCH simply is not supported then...

But of course you are free to ask to add support for method PATCH as a product suggestion here, because it seems to be used more frequently with REST APIs... Just add it as a separate question with tag product-suggestion or the like...

(04 Apr '20, 06:37) Volker Barth

what is meant with the OPTIONS method?

I added a product-suggestion!

(04 Apr '20, 20:10) Baron

The OPTIONS method has been added with v17, see the docs. I guess it's main use is for CORS.

(05 Apr '20, 08:32) Volker Barth
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×48
×31
×30

question asked: 03 Apr '20, 08:11

question was seen: 1,265 times

last updated: 05 Apr '20, 08:32