# Converting Postman Prerequest Script to Julia Script

**URL:** https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832
**Category:** Web Stack
**Tags:** web
**Created:** [October 15, 2021, 3:19pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832 "2021-10-15T15:19:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bww00](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@bww00](https://discourse.julialang.org/u/bww00)
#### Post date: [October 15, 2021, 3:19pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832/1 "2021-10-15T15:19:26Z")

</div>

I would like to convert the following postman js script to julia http requests.  
We will then translate to a vendor supplied programming language.  
The Julia code would be used to validate the vendor supplied results

Thanks for any help

The following is a postman prerequest script (values changed)

var isoTime = new Date().toISOString();  
var entitySecret = “ff26516ecb32b123456dd0f42aa5106f237a69c806”;  
var partnerSecret = “77eb0f2ad699c123456cb8677452212fed9b61a3c231”;  
var patientId = “1006”;

var digest = “POST\n/clinxxxx-yyyyyyy-zzzzzzzzzz/v1/resultservice/aaaaaaaaaaaaa\n”;  
digest += “US\n”;  
digest += isoTime + “\n”;  
digest += pm.request.headers.get(“bbbb-Entity”) + “\n”;  
digest += pm.request.headers.get(“bbbb-Partner”) + “\n”;  
digest += pm.request.headers.get(“bbbb-Partner-Product”) + “\n”;  
digest += pm.request.headers.get(“bbbb-User”) + “\n”;  
digest += indtId + “\n”;

var hash = CryptoJS.HmacSHA256(digest, partnerSecret + entitySecret);  
var hashHeader = CryptoJS.enc.Base64.stringify(hash);

---

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [October 15, 2021, 6:35pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832/2 "2021-10-15T18:35:45Z")

</div>

Hey @bww00 - would you mind showing us what you have tried so far? i would suggest taking a look at the following packages to get started with:

- [Home · HTTP.jl](https://juliaweb.github.io/HTTP.jl/stable/)
- [Home · JSON3.jl](https://quinnj.github.io/JSON3.jl/stable/)

Also, it appears that you have actual secret keys in your example code - is that an accident? With `var entitySecret`?

Particularly, with HTTP.jl, check out the `get` and `post` functions as well as the generic `request` command. That should help get you started if you are unsure where to start.

---

<div class="post-metadata">

### Author: ![bww00](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@bww00](https://discourse.julialang.org/u/bww00)
#### Post date: [October 15, 2021, 6:47pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832/3 "2021-10-15T18:47:20Z")

</div>

Thanks,  
The keys have been altered and are not the actual keys.  
I will look deeper at Http.jl and JSON3.jl

For the crypto i found nettle.jl that had hmac functionality

regards  
bww00

---

<div class="post-metadata">

### Author: ![bww00](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@bww00](https://discourse.julialang.org/u/bww00)
#### Post date: [October 18, 2021, 1:44pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832/4 "2021-10-18T13:44:43Z")

</div>

some progress  
I can connect to site and get 401 return.  
Now I need to create the Hmacsha256 and base 64 encode the hash and add it to the headers as authorization  
Hopefully verify the hmacsha256 and encode vs the postman example that works.

urla = “[https://test12.services.xxxx.com/resultofservice/v1/resultservice/resultof](https://test12.services.xxxx.com/resultofservice/v1/resultservice/resultof)”  
response = HTTP.request(“POST”, urla,  
[“Content-Type” =\> “application/json”  
, “xxxX-Country” =\> “US”, “xxxX-Entity” =\> “br54976ac9c93863”,  
“xxxX-zzzzzzz” =\> “br54903a686a9030”, “xxxX-User” =\> “testuser”,  
“xxxX–zzzzzzz-Product” =\> “1.0”, “xxxX-Date” =\> “2021-10-13T17:52:21.527Z”,  
“Authorization” =\> “xxxxxx”  
],

```
        JSON.json(params_body); verbose=4)

```

response\_text = String(response.body)  
json\_obj = JSON.parse()  
display(JSON.parse(response\_text))

---

<div class="post-metadata">

### Author: ![bww00](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@bww00](https://discourse.julialang.org/u/bww00)
#### Post date: [October 19, 2021, 2:24pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832/5 "2021-10-19T14:24:35Z")

</div>

Got it to work!!!

encoding issue was below that I found searching

For this you want to use Base64 from the standard library. First use hex2bytes to convert your string to a byte array, then base64encode to convert it back to a string:  
using Base64  
function base16to64(st::AbstractString)  
bytes = hex2bytes(st)  
return base64encode(bytes)  
end

---

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [October 19, 2021, 4:16pm UTC](https://discourse.julialang.org/t/converting-postman-prerequest-script-to-julia-script/69832/6 "2021-10-19T16:16:32Z")

</div>

Congrats @bww00 ! For posterity, may I suggest wrapping your code blocks on your comments in backticks (these things: ``) to make your code nicely formatted? This can help others who come after who might have the same question as you. Also, since you have found your solution, I’d suggest also marking your solution 🙂
