HttpCommand

MiServer is Dyalog's APL-based web development framework

HttpCommand

Postby Gantois on Sat May 08, 2021 11:11 pm

Hello,
I would like to use HttpCommand instead of curl executed by ⎕cmd but I have some doubts.
I am trying to reproduce an example that works if processed from within DOS prompt command line. I am grateful if anyone can help me.

My test:
cmd←⎕NEW HttpCommand
cmd.URL←'https://api.hml.pb.blupay-apps.com.br/api/bank-slips?withoutPix=false'
cmd.Headers←(('Authorization' 'Bearer <token>')('Accept' 'application/json'))

cmd.Command←'????'
result←cmd.Run

1. what is the instruction for setting cmd.Command?
2. What is the instruction for setting the "raw data" based in the example below?

Thanks,
Marco Gantois


curl --location --request POST "https://api.hml.pb.blupay-apps.com.br/api/bank-slips?withoutPix=false"
--header "Authorization: Bearer ',token,'"
--header "Content-Type: application/json"
--data-raw "{
....
.....
...

}
Gantois
 
Posts: 80
Joined: Thu Apr 17, 2014 9:44 am

Re: HttpCommand

Postby Brian|Dyalog on Sun May 09, 2021 11:28 pm

Hi Marco!

HttpCommand can do what you want it to. Let's suppose that the payload data is just a JSON string and you assign the other elements of the request to variables. For example:
      payload←'{"name1":"value1","name2":{"name3":"value3", ... }'
url←'https://api.hml.pb.blupay-apps.com.br/api/bank-slips?withoutPix=false'
hdrs←(('Authorization' 'Bearer <token>')('Accept' 'application/json'))
command←'POST'

Now you have a lot of flexibility on how to execute the request; all of the following are equivalent. The response payload is found in r.Data.
      ⍝ use the constructor arguments
cmd←⎕NEW HttpCommand command url payload hdrs
r←cmd.Run

⍝ assign the individual parameters
cmd←⎕NEW HttpCommand
cmd.(Command URL Params Headers)←command url payload hdrs
r←cmd.Run

⍝ use HttpCommand.Do
r←HttpCommand.Do command url payload hdrs

Finally, if both the request and response payloads are JSON, you can use HttpCommand.GetJSON and HttpCommand will convert your request APL array/object to JSON and then convert the response JSON into an APL array/object. You also don't need to specify the content-type header. For your example, the APL object would be the equivalent of ⎕JSON payload.
      aplpaylaod←⎕JSON payload ⍝ request payload is now an APL object/array
hdrs2←1 2⍴'Authorization' ('Bearer ',token)
r←HttpCommand.GetJSON command url aplpayload hdrs


I hope this helps!
/Brian
User avatar
Brian|Dyalog
 
Posts: 116
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: HttpCommand

Postby Gantois on Wed May 12, 2021 1:11 pm

[andys|dyalog has edited this post 2021-05-12 16:27 to remove the credential details which were included in the post]

Thanks Brian,

The instructions worked nice.
Thanks very much.

There are another type of instruction used one step before the example you solved, whose goal is get a token (credential) from BluePay.

--data-urlencode "grant_type=client_credentials"

How could I use it in HttpCommand?
In this case there are no payload. I tryed put it in HttpCommand as payload and as header too, unsucessfuly.

Could you help me again, please?
Marco Gantois


Following the original function that I am trying to use with HttpCommand:

∇ r←Autenticar;x
⍝ Autenticar - retorna o token
:Access Public
x←'curl --location --request POST "https://hml.auth.blupay-apps.com.br/auth/realms/payment-bus/protocol/openid-connect/token" '
x,←'--header "Authorization: Basic <bearer_token>" '
x,←'--header "Content-Type: application/x-www-form-urlencoded" '
x,←'--data-urlencode "grant_type=client_credentials"'
x←⊃⎕cmd x
js←0 ⎕json x
r←js.access_token
Gantois
 
Posts: 80
Joined: Thu Apr 17, 2014 9:44 am

Re: HttpCommand

Postby Brian|Dyalog on Wed May 12, 2021 3:47 pm

Hi Marco!

In this case, you can pass the name/value pair in the Params element of the HttpCommand. HttpCommand will automatically set the content-type and URLEncode the payload.
      url←'https://hml.auth.blupay-apps.com.br/auth/realms/payment-bus/protocol/openid-connect/token'
hdrs←'Authorization' 'Basic <bearer token>'
params←'grant_type' 'credentials'
response←HttpCommand.Do 'post' url params hdrs
js←0 ⎕JSON response.Data
r←js.access_token


I hope this helps!
/Brian
User avatar
Brian|Dyalog
 
Posts: 116
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: HttpCommand

Postby Gantois on Wed May 12, 2021 10:19 pm

Hi Brian!

It worked

Thanks very much
Gantois
 
Posts: 80
Joined: Thu Apr 17, 2014 9:44 am

Re: HttpCommand - Returnig Binary File

Postby Gantois on Thu May 13, 2021 2:13 pm

Hi,

Are there a specific HttpCommand instruction/parameter when the response is a binary file and not a JSON?
Is the response an object as it is when using json payload (response.Data)?

Att... Marco Gantois


Considering that nothing is necessary to change, I did the following test. The expected response shoud be a binary file to be converted to a pdf later.

Could the error be the result of a wrong command that I made?

Thanks,
/Marco Gantois

url←'https://api.hml.pb.blupay-apps.com.br/api/bank-slips/',js.id,'/pdf'
cmd←'GET'
hdrs←'Authorization' ('Bearer ',token)
response←#.HttpCommand.Do cmd url hdrs
js←0 ⎕json response.Data


It not worked:
response.HttpMessage
Unauthorized
response.HttpStatus
401

ps: the same token variable was used successfully in another Http Command previously (some instructions before)
Gantois
 
Posts: 80
Joined: Thu Apr 17, 2014 9:44 am

Re: HttpCommand

Postby Brian|Dyalog on Fri May 14, 2021 2:17 pm

Hi Marco,

Retrieving a binary file should be no problem for HttpCommand. The data is returned in the response.Data element. You should examine the content-type header in response.Headers and then based on that, decide what to do. If the server returned a PDF file, the content-type is most likely application/pdf and you can simply save response.Data to a native file. In your example you were trying to use ⎕JSON on the response.Data. You should only use ⎕JSON if the content-type of the response is application/json.

But in order to do that, you first need to address the 401 HTTP Status...

The 401 Authorization Error means that the server rejected the request because the credentials failed to authenticate before it tried retrieving the resource. I don't know anything about the API that you're using, so the best I can do is give you some general guidance. If you want specific help on this, it might be better to do it in email, or perhaps we can schedule a Zoom session.

I assume you're getting the name/id of the resource to retrieve from an earlier request.Some things you should look at.
  • Look in the headers of the earlier response and see if the server sent some sort of cookie that should be included in subsequent requests
  • Examine the headers in the 401 response to see if there some indication of the problem. There may be a WWW-Authenticate header which may yield some useful information
  • Examine the data in the 401 response, it may contain information about why the authentication failed.
  • Review the documentation for the API you're using.

Here's a link to decent article on 401 errors. Although it's more focused on using a browser for the client, some of the information would still apply when using HttpCommand.

I hope this helps!
/Brian
User avatar
Brian|Dyalog
 
Posts: 116
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: HttpCommand

Postby Gantois on Fri May 14, 2021 3:01 pm

Thanks Brian,
I will search.
/MG
Gantois
 
Posts: 80
Joined: Thu Apr 17, 2014 9:44 am


Return to MiServer

Who is online

Users browsing this forum: No registered users and 1 guest