4

How to Upload Media via the WP REST API ?

I am trying to upload media to

http://example.com/wp-json/wp/v2/media

using POSTMAN.

I tried uploading image using the following headers

Content-Type: image/jpeg
Content-Disposition: attachment; filename=name.jpeg

and the payload I am sending is

source_url: (file) Uploaded File
post: (text) Post Id

and the response that I'm getting is

{
    "code": "rest_upload_unknown_error",
    "message": "File is empty. Please upload something more substantial. 
                This error could also be caused by uploads being disabled in your php.ini or 
                by post_max_size being defined as smaller than upload_max_filesize 
                in php.ini.",
    "data": {
        "status": 500
    }
}

and When I tried sending the file path instead of the file like

source_url: (text) file:///C:/Users/Username/Pictures/01.jpg
post: (text) Post Id

I get the response as

{
    "code": "rest_upload_no_data",
    "message": "No data supplied.",
    "data": {
        "status": 400
    }
}

I tried uploading image via the web dashboard and it works. Just got no clue as to how to make it work via the API.

Any suggestions or help appreciated!
Thank You!

4
  • Where did you read that you should send source_url and post as the payload? That doesn't sound correct at all. You should just pass the file itself in the body. See also this duplicate question.
    – swissspidy
    Feb 21, 2018 at 14:05
  • @swissspidy I did referred to that question, could you tell me how to send the file in the body? I mean what key should be assigned to the file so that I could test it via POSTMAN? Feb 22, 2018 at 4:17
  • What key? There's no key when you select "binary" for the body.
    – swissspidy
    Feb 22, 2018 at 11:32
  • @swissspidy finally i was able to upload media, I used the key as file instead of source_url Feb 22, 2018 at 11:40

1 Answer 1

2

If you still don't have a solution using Postman, I was able to send .png format image by configuring with these headers in POST:

Postman Headers Wordpress API V2 Media Upload

And then in the body you select the binary option. So that worked for me. Postman Body Wordpress API V2 Media Upload

1
  • just curious, how would I proceed with making it Binary instead of Form when I do the call from my nextjs app? Is there a header parameter to add or will I convert my file upload to binary? Sep 14, 2023 at 2:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.