• I’m trying to upload files to a localhost blog using WordPress’ API and Java. This is the code I have:

    private void postMedia(){
                String token = (String)Application.getInstance().getModel().getBean(Costants.BEARER_TOKEN);
                try {
                    CloseableHttpClient httpClient = HttpClients.createDefault();                
                    String urlFile = "C:\\Users\\Documents\\Project\\src\\res\\test_picture.jpg";
                    HttpEntity entity = MultipartEntityBuilder.create()
                            .addTextBody("title", "title")
                            .addTextBody("caption", "description")
                            .addBinaryBody("upload_file", new File(urlFile), ContentType.create("image/jpeg"), "test_picture.jpg")
                            .addTextBody("status", "publish")
                            .build();
    
                    HttpPost httpPost = new HttpPost(Constants.URL_MEDIA);
                    httpPost.setEntity(entity);
                    System.out.println("Request Type: "+ httpPost.getMethod());
                    httpPost.setHeader("Authorization", "Bearer " + token);
                    HttpResponse response = httpClient.execute(httpPost);
                    HttpEntity result = response.getEntity();
                    Scanner sc = new Scanner(response.getEntity().getContent());
                    System.out.println(response.getStatusLine());
                    while(sc.hasNext()) {
                        System.out.println(sc.nextLine());
                    }
                    } catch (Exception e) {
                    e.printStackTrace();
                }   

    Constants.URL_MEDIA is http://localhost/tesi/wp-json/wp/v2/media

    The output I get from the scanner is:

    Request Type: POST
    HTTP/1.1 500 Internal Server Error
    {"code":"rest_upload_unknown_error","message":"Specified file failed upload test.","data":{"status":500}}

    What am I missing or doing wrong?

    • This topic was modified 6 years, 5 months ago by James Huff. Reason: moved to Locahost Installs forum
Viewing 1 replies (of 1 total)
  • Tyler

    (@tylerthedude)

    Hi there,

    You might try changing the code from:

    .addBinaryBody("upload_file",

    to something such as:

    addBinaryBody("file" [...]

    Regards,
    Tyler

Viewing 1 replies (of 1 total)

The topic ‘“Specified file failed upload test” error while uploading files via API’ is closed to new replies.