## rest api – 403 Forbidden when Updating (I presume POST?) but not Creating (PUT?) an image

Question

I’m using the WordPressPCL library for .NET alongside WooCommerce (although my problem is with the WordPress API, not the WooCommerce API), and trying to upload an image alongside my product

My current process is

1. Upload the image using WordPressPCL, using the Media.Create() method which I presume PUTs the image
2. Create the product using WooCommerceNET, with the image’s URL as the product URL
3. Update the image, setting the Media’s “Post” ID to the WooCommerce product ID

Steps 1 and 2 work fine and I can upload dozens of products, but on 3 I get a 403 Forbidden response which doesn’t make sense to me since I was able to upload the image.

The above order is important to me, because I absolutely do not want to create a product without the image already being on the server – therefore I need to guarantee the process fails if there’s a problem with either the image upload or the product upload. Hence I update the image to attach to the post afterwards.

I know that you don’t strictly need to attach the image to a post, but it’s a requirement from the customer, who uses the attachment to manage deletion of images.

Simplified code below (I’ve removed the details of error handling, object creation etc)

// Create the image
try
{
}
catch
{
// Image upload failed. Fail the process
}

// Image uploaded successfully, now create the product
try
{
productImage = new ProductImage()
{
};

// Replace with the product recieved from the server so that

}
catch(Exception e)
{
// Product creation failed, fail the process (and try to remove the image, but don't worry about it)
}

// Now try to link the image to the post
try
{