admin管理员组

文章数量:1195902

How to Upload Media via the WP REST API ?

I am trying to upload media to


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!

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!

Share Improve this question asked Feb 21, 2018 at 13:49 linktoahreflinktoahref 1831 gold badge2 silver badges8 bronze badges 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 Commented 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? – linktoahref Commented Feb 22, 2018 at 4:17
  • What key? There's no key when you select "binary" for the body. – swissspidy Commented Feb 22, 2018 at 11:32
  • @swissspidy finally i was able to upload media, I used the key as file instead of source_url – linktoahref Commented Feb 22, 2018 at 11:40
Add a comment  | 

1 Answer 1

Reset to default 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:

And then in the body you select the binary option. So that worked for me.

本文标签: WordPress REST Upload Media