04-27-2021 09:16 AM
Hi,
Hoping someone can offer some advice around this for me.
Problem: I have a snap pipeline set up as a task in our environment, when called with a param it will return some json data around customer information. I can test this via postman with cloud and locally on my machine with the browser using both urls (on prem and cloud). However when using my web application (React) and using fetch() - I’m getting the 401 (Unauthorized) error in the console.
Should i be using Basic Auth or the Bearer Token?
Both of which seemed to be failing and nothing under in the headers (inspected the network tab in dev tools)
Preview of my code making the request
fetch(url, {
mode: 'no-cors',
headers: new Headers({
"Authorization": `Basic ${base64.encode(`${login}:${password}`)}`
}),
})
Any help would be greatly appreciated thanks.
Regards
Rich
04-27-2021 11:16 AM
Hi Rich:
You should be passing the generated bearer token for the task. Something like
Authorization: Bearer 9V9HGRt0m9BRgD42AZWpzC14QuYABCDE
Is this a triggered or Ultra task ?
04-28-2021 12:55 AM
I had also tried that approach passing the
"Authorization" : "Bearer {token_value}"
But still receiving a 401. I did notice that the browser asked for my credentials where as postman didn’t. Hence why i tested with basic.
I am using a triggered task, unfortunately I’m not clued up on the Ultra Tasks - is this something i should be using in this scenario?
Thanks for your response
Regards
Rich
04-28-2021 12:56 PM
Hi Rich:
A triggered task should be fine for what you’re doing. An Ultra pipeline/task runs constantly and is meant for real-time processing. You can read more about it here.
https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1438189/Triggered+Tasks
If you don’t supply a bearer token, then you need to supply your SnapLogic credentials. It sounds like neither is working you (that is, you’re getting a 401).
I’ve verified a simple triggered task using Curl and also a Java client. I assume the bearer token is valid and you have verified the authorization header is written into the request for your React app.
Here is curl request with a valid bearer token:
$ curl -verbose -X POST \
-H "Authorization: Bearer T3gwZuqhcc1no4ZBltrZi4XVTqiPwfdP" \
-H "Content-Type: application/json" \
-d '{"param1": "value"}' \
http://sm-mbp-mbowen:8888/api/1/rest/slsched/feed/snaplogic/projects/support/simple-task-1
REQUEST:
> POST /api/1/rest/slsched/feed/snaplogic/projects/support/simple-task-1 HTTP/1.1
> Host: sm-mbp-mbowen:8888
> User-Agent: curl/7.54.0
> Accept: */*
> Referer: rbose
> Authorization: Bearer T3gwZuqhcc1no4ZBltrZi4XVTqiPwfdP
> Content-Type: application/json
> Content-Length: 19
RESPONSE:
< HTTP/1.1 200 OK
...
[
{"content":{"param1":"value"}}
]
A curl request with my SnapLogic credentials:
$ curl --user mbowen@snaplogic.com:<password> -verbose -X POST \
-H "Content-Type: application/json" \
-d '{"param1": "value"}' \
http://sm-mbp-mbowen:8888/api/1/rest/slsched/feed/snaplogic/projects/support/simple-task-1
REQUEST:
> POST /api/1/rest/slsched/feed/snaplogic/projects/support/simple-task-1 HTTP/1.1
> Host: sm-mbp-mbowen:8888
> Authorization: Basic YWRtaW5Ac25hcGxvZ2ljLmNvbTpBZG0xbkAxMno=
> User-Agent: curl/7.54.0
> Accept: */*
> Referer: rbose
> Content-Type: application/json
> Content-Length: 19
RESPONSE:
< HTTP/1.1 200 OK
...
[
{"content":{"param1":"value"}}
]
I will get a 401 if either the token or credentials aren’t valid.
< HTTP/1.1 401 UNAUTHORIZED
{"response_map": {"error_list": [{"message": "Authentication required"}]}, "http_status_code": 401}
It sounds like you are able to trigger the task via Postman, so you’re passing the right authentication bits. Is your browser configured to use a proxy? Seems something with the React app.