This was a fun one to track down and kind of humorous.
I was using PageImage::add() to add an image by URL and running into the exception Unable to install invalid image. I dug into the code and tried to duplicate the individual steps using WireHttp::download().
It seemed to work. The file was created, but it was only 1kb. I checked the HTTP status code and it was 418 and the file was just a bit of JSON {"error": "I'm a teapot: no user-agent sent with request"}. I was still confused why WireHttp didn't throw an exception, though, and finally found that it's checking for a code >= 400 AND that the status code is in the list.
418 is a joke status code but has been implemented at times; apparently in the GoToSocial ActivityPub software in this case. 😆 I'm not sure the best course of action, but I think this conditional check should be loosened so it catches status codes that might not be in the list. Perhaps:
if(count($this->error) || ($this->httpCode >= 400)) {
getError() would probably also need to be loosened up to return just the HTTP code if it isn't in the list.
It might also be worthwhile to add a default User Agent to requests; could be something like ProcessWire/[hostname].
This was a fun one to track down and kind of humorous.
I was using
PageImage::add()to add an image by URL and running into the exceptionUnable to install invalid image. I dug into the code and tried to duplicate the individual steps usingWireHttp::download().It seemed to work. The file was created, but it was only 1kb. I checked the HTTP status code and it was 418 and the file was just a bit of JSON
{"error": "I'm a teapot: no user-agent sent with request"}. I was still confused why WireHttp didn't throw an exception, though, and finally found that it's checking for a code>= 400AND that the status code is in the list.418 is a joke status code but has been implemented at times; apparently in the GoToSocial ActivityPub software in this case. 😆 I'm not sure the best course of action, but I think this conditional check should be loosened so it catches status codes that might not be in the list. Perhaps:
getError()would probably also need to be loosened up to return just the HTTP code if it isn't in the list.It might also be worthwhile to add a default User Agent to requests; could be something like
ProcessWire/[hostname].