Woocommerce product api v3 not accept urls images for 9191 port. Only 8080

My POST request (JSON to create PRODUCT IN WOOCOMMERCE API V3) fail for images.

I have the same image exposed to the internet through 2 different ports. https fails too

---------------- Fail: not Works for 9191 or other port --------------------------------

{
          "src": "http://190.64.76.10:9191/img/logo.png"
}

Error

{
"code": "woocommerce_product_image_upload_error",
"message": "Error getting remote image http://190.64.76.10:9191/img/logo.png. A valid URL has not been provided",
"data": {
    "status": 400
}

}

---------------- Works fine with 8080 port --------------------------------

{
          "src": "http://190.64.76.10:8080/img/logo.png"
}

Any ideas ?????

Upvotes: 2

Views: 1044

Answers (1)

CherryDT
CherryDT

Reputation: 29011

Internally, the URL is validated by calling wp_http_validate_url. Among other rules, it checks whether the port is one of 80, 443 or 8080:

$port = $parsed_url['port'];
if ( 80 === $port || 443 === $port || 8080 === $port ) {
    return $url;
}

You can see the relevant part of the code on GitHub here. There is also an issue where some had the same problem, which you can find here.

So, bottom line, by default you just cannot use any other port than 80, 443 or 8080. There is no way to change that other than disabling the URL validation in WordPress, which is probably not a good idea security-wise.

But if you really need it, you can do it by adding the following code in a plugin or theme in your WordPress installation:

add_filter('http_request_args', function ($args, $url) {
  $args['reject_unsafe_urls'] = false;
  return $args;
}, 50, 2);

Upvotes: 3

Related Questions