skip to Main Content

after many times I’m searching on the Internet.

I find the function to upload new media with REST API WP with CURL in PHP.

but, I don’t know how to convert it to POSTMAN to test faster.

this’s my PHP Code worked.

$path = 'D:WALLPAPERlandscape_natural_Beautiful_mountain_scenery_house_green_2560x1600.jpg';
$request_url = 'http://bdshuuloc.phongmy.net/wp-json/wp/v2/media';

$image = file_get_contents( $path );
$mime_type = mime_content_type( $path );

$user = 'thienduc';
$pass = '@#ThienDuc#@';

$api = curl_init();
curl_setopt( $api, CURLOPT_URL, $request_url );
curl_setopt( $api, CURLOPT_POST, 1 );
curl_setopt( $api, CURLOPT_POSTFIELDS, $image );
curl_setopt( $api, CURLOPT_HTTPHEADER, array( 'Content-Type: ' . $mime_type, 'Content-Disposition: attachment; filename="' . basename($path) . '"' ) );
curl_setopt( $api, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $api, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt( $api, CURLOPT_USERPWD, $user . ':' . $pass );
$result = curl_exec( $api );
curl_close( $api );

$rs = json_decode( $result );
print_r( $rs);

And this’s config on POSTMAN: => https://prnt.sc/10cifq3

Basic Auth is ok. I can delete and update, add a new post success

how to convert or add some condition in POSTMAN to work for UPLOAD MEDIA with REST API, everybody.

Thanks 🙂

2

Answers


  1. Here is the way to copy HTTP requests from Developer tools to Postman:

    1. With the network tab open, right click on the request and select ‘copy as Curl’
    2. Next open up Postman and click on Import.
    3. Select past raw text and copy in the Curl request.
    4. Click import and your request will be imported to use and save in collections.
    Login or Signup to reply.
  2. In case anyone today is still looking for an answer, the answer in this post helped me:
    https://wordpress.stackexchange.com/questions/294725/wordpress-rest-upload-media

    Headers:

    • Content-Disposition -> form-data; filename="yourFileName.jpg"
    • Content-Type -> image/jpeg

    Body:

    • select -> binary and select your image
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search