Add media with WP-Rest-API v2 II

I suspect based on an answer at StackOverflow that something like this would work:

base64credentials="...... "
curl --request POST \
--url "http://www.yoursite.com/wp-json/wp/v2/media" \
--header "cache-control: no-cache" \
--header "content-disposition: attachment; filename=tmp" \
--header "authorization: Basic $base64credentials" \
--header "content-type: image/png" \
--data-binary "@/home/web/tmp.png" \
--location

This should use tmp.png to create and upload a brand new attachment. You can then parse the result to retrieve the ID or success/failure and do other things with it, such as setting featured image meta etc.

I did notice the capitalisation of Authorization needed adjusting, and you’ll want to adjust the content type and data binary parameters to match your target file, let me know how that goes

https://stackoverflow.com/questions/37432114/wp-rest-api-upload-image

First, your content-type should be image/jpeg and not application/json, remember that content-type is supposed to reflect the data that you are passing and the POST media request expects an image.

Another change you have to make to accommodate the content-type is the way that you are passing the data. Instead of sending it with the source_url parameter, try passing it as a binary file.

One last thing I would mention is that the wp/v2 calls return 3XX status on a few occasions. It would be useful to follow those redirects and redo those requests to those new URLs.

Leave a Comment