3

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!

  • 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 '18 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? – linktoahref Feb 22 '18 at 4:17
  • What key? There's no key when you select "binary" for the body. – swissspidy Feb 22 '18 at 11:32
  • @swissspidy finally i was able to upload media, I used the key as file instead of source_url – linktoahref Feb 22 '18 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.