3

how to upload media-image or featured-image in wordpress using rest api.

I've created new post using WordPress REST API, and now I am uploading Image to my Wordpress Site using REST API but i am unable to achieve it due to error "No data Supplied" kindly please Check Screenshot

enter image description here

which is the best way to create new post in WordPress with Featured Image, right now in my mind

  1. Upload Media File to WordPress Site and Get Media ID
  2. Create New Post with Media ID
1
  • I dont see images so I added a complete code in answer Commented Jan 9, 2020 at 20:56

3 Answers 3

7

you almost right, just lack media(image) raw binary data.

for python, using code:

import requests

toUploadImagePath = "/xxx/xxx.jpg"
mediaImageBytes = open(toUploadImagePath, 'rb').read()
# b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\.....'

uploadImageFilename = "661b943654f54bd4b2711264eb275e1b.jpg"
curHeaders = {
  "Authorization": "Bearer xxx.yyy.zzz-xxx-yyy-zzz",
  "Content-Type": "image/jpeg",
  "Accept": "application/json",
  'Content-Disposition': "attachment; filename=%s" % uploadImageFilename,
}

resp = requests.post(
  "https://www.crifan.com/wp-json/wp/v2/media",
  headers=curHeaders,
  data=mediaBytes,
)

full code can refer my lib: crifanWordpress.py

and my Chinese post: 【已解决】用Python通过WordPress的REST API上传图片 (will publish in short future)

Sign up to request clarification or add additional context in comments.

1 Comment

NOTE: We were getting http status 503 when posting a file that was about 0.5 MB in size. I shrank the file to 20K and no issues.
6

Using the REST API to upload a file to WordPress is quite simple. All you need is to send the file in a POST-Request to the wp/v2/media route.

UPDATED added data response true

$file = file_get_contents( 'test.jpg' );
$url = 'http://example.com/wp-json/wp/v2/media/';
$ch = curl_init();
$username = 'admin';
$password = 'password';

curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
    'Content-Disposition: form-data; filename="example.jpg"',
    'Authorization: Basic ' . base64_encode( $username . ':' . $password ),
] );
$result = curl_exec( $ch );
curl_close( $ch );
print_r( json_decode( $result ) );

MORE https://gist.github.com/ahmadawais/0ccb8a32ea795ffac4adfae84797c19a

2 Comments

How can we create a post with media in it ? provided media url from extenal source
@Srinivas08 there's no API option to send url. but you can do by creating custom endpoint to upload media using media_sideload function
0

I landed here looking for answers too. But I finally mixed and matched PHP and Nodejs. @user1805543's answer was my php code (thanks man). I returned the id from $result. (remember to json_decode($result, true)) All that was left to do was to make a get request to that where that php was running - localhost:YOUR_PORT

I used axios in my case, so inside the callback I received the id, and I proceeded to make post request for new blog post, supplying the id as featured_media

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.