Error: The URL is invalid

Scriptable is saying the URL is invalid when I try to make a request of this URL:

This is the code I’m using:

// Get all contacts where the 1st character in notes field is @
const contacts = (await Contact.all(await ContactsContainer.all())).filter(c=>c.note[0]=='@');

// Loop through contacts
for(var i=0;i<contacts.length;i++){
  var req = new Request('');
  req.method = 'POST';
  req.body = `submit=${contacts[i].note.substr(1)}`;
  var imageURL = ((await req.loadString()).split("<center><img src='")[1].split("'></center>")[0].replace(/&amp;/g, '&'));
  var req = new Request(imageURL);
  var res = await req.loadImage() // Error: The URL is invalid.

Could anyone help me?

2 things I can think of:

What’s the exact URL it is trying to load? Yes, you’ve posted a URL, but in the code you split it at ?. Maybe there are some escapes in the URL that render it invalid. (just pass the same argument to the log() function)

When making a GET request, there is no body to send with it. It probably ignores it, but try it without assigning anything to the body.

I just tried doing var req = new Request(imageURL); but the same error appears.

What is the content of imageURL? Apparently there must be something in it that Scriptable doesn’t like.
You could try instead of the request to see if Safari can load it. If it can’t, then the URL is definitively faulty. If Safari can load it, then it maybe there is a bug in Scriptable.

I don’t think you are trying to retrieve what you think you are.

I’m pretty sure that this simplified version gets the image just fine; obviously I can’t check it against your contacts.

let strURL = "";
let objReq = new Request(strURL);
let objImg = await objReq.loadImage()

One thing I noted was that you were defining req twice. I don’t think that’s an ideal approach, and is generally why let is favoured over var in general use as the JIT then won’t let you define it twice. Saying that, even if set the code as follows, it still actually seems to work for me.

let strURL = "";

//Declare as request as var and use it
var objReq = new Request('');
objReq.method = 'POST';
await objReq.loadString();

//Redeclare it and use it for the image
var objReq = new Request(strURL);
let objImg = await objReq.loadImage()

I hope that something in the above helps.

The problem is I’m using the first request so I can the URL for the second request.

Change your loadImage() temporarily to a load(), and then look at the response attribute of the request. I’ve tried a few things and had forbidden permissions and invalid timestamps trying to re-use an example URL.