Make an authenticated JSON request

I see this page
https://p5js.org/reference/#/p5/httpDo

But when I try to make an API request with my access token from https://littlesis.org/api it doesn’t work.

I see that LittleSis wants the string “Littlesis-Api-Token:” but I don’t know how to insert that into the header. All I see in examples is “Bearer XXXXXX”, should that work here? I’ve tried a few things and no avail. Any ideas?

Possibly helpful?:

1 Like

Thanks for the suggestion! It looks like the API he’s using allows you to pass the key via the URL whereas for littlesis you need to do it in a special way.

Here’s a rough example of how to include the API Token in the request header:

function preload() {
  let url = 'https://littlesis.org/api/entities/1';
  httpDo(
    url,
    {
      method: 'GET',
			// Other Request options, like special headers for apis
      headers: { 'littlesis-api-token': 'YOUR_TOKEN_HERE'},
			mode: 'no-cors',// no-cors, *cors, same-origin
			credentials: 'include',// include, *same-origin, omit
			redirect: 'follow', // manual, *follow, error
    	referrer: 'client', // no-referrer, *client
    },
    function(res) {
      console.log(res);
    },
		function(err) {
			console.log(err);
		}
  );
}

function setup() {
	createCanvas(300, 300);
}

function draw() {

}

I am getting CORS related errors and unfortunately unable to provide a quick solution, but I’m sure the more web experienced will provide advice there.

As a hacky backup-plan I can suggest using a basic server side script to cache the request locally so p5.js can access it. (e.g. a node script that receives the same request you would to little-sister, does the request and funnels the message back, but from localhost/same domain the p5 sketch is served from)

1 Like

Thanks so much for taking the time to help out.

This looks a bit better than what I had worked out, but I get a 422 Error, “No Reason Phrase”, any idea what that’s about?

https://httpstatuses.com/422

Not sure, but possibly you submitted a well-formed request that is against their policies? Like for example:

You may want to consult