O
O
Orkhan Hasanli2018-11-09 03:01:08
Java
Orkhan Hasanli, 2018-11-09 03:01:08

Why is the WordPress Rest API client not working?

Good day!
Has anyone had to work with the following client in Java?
https://github.com/Afrozaar/wp-api-v2-client-java
In short, I'm trying to publish articles from Spring using the WP REST API.
Here is a simple code for testing:

public class WpTest {
    public static void main(String[] args) {

        String baseUrl = "http://example.com";
        String username = "customUser";
        String password = "customPassword";
        boolean usePermalinkEndpoint = false;
        boolean debug = true;

        final Wordpress client = ClientFactory.fromConfig(ClientConfig.of(baseUrl, username, password, usePermalinkEndpoint, debug));

        String expectedTitle = "Demo Title";
        String expectedExcerpt = "Demo Excerpt";
        String expectedContent = "Demo Content";

        final Post post = PostBuilder.aPost()
                .withTitle(TitleBuilder.aTitle().withRendered(expectedTitle).build())
                .build();

        try {
            final Post createdPost = client.createPost(post, PostStatus.publish);
        } catch (PostCreateException e) {
            e.printStackTrace();
        } 
    }
}

As a result, I see an exception:
Exception in thread "main" java.lang.RuntimeException: Cannot create ParsedRestException.
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input at [Source: (byte[])""; line: 1, column: 0]

Where is my mistake?
Library Version 4.8.3 VI Version 4.9.8
Gradle -
implementation group: 'com.afrozaar.wordpress', name: 'wp-api-v2-client-java', version: '4.8.3'

Full log:
full log
Exception in thread "main" java.lang.RuntimeException: Can not create ParsedRestException.
  at com.afrozaar.wordpress.wpapi.v2.exception.ParsedRestException.of(ParsedRestException.java:105)
  at com.afrozaar.wordpress.wpapi.v2.exception.PostCreateException.<init>(PostCreateException.java:8)
  at com.afrozaar.wordpress.wpapi.v2.Client.createPost(Client.java:181)
  at com.afrozaar.wordpress.wpapi.v2.Client.createPost(Client.java:187)
  at info.md7.wpat.backend.wpRest.WpTest.main(WpTest.java:39)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: (byte[])""; line: 1, column: 0]
  at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
  at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4145)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3091)
  at com.afrozaar.wordpress.wpapi.v2.exception.ParsedRestException.of(ParsedRestException.java:98)
  ... 4 more


PS I'm checking on a clean installed VP and without plugins with a default theme...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2018-11-09
@azerphoenix

So, I figured out the problem.
The problem is that some shared hostings disable http authorization by default.
Below I attach links to useful resources that helped me in resolving the problem:
1) here are described possible edits in htaccess
https://github.com/WP-API/Basic-Auth/issues/35
2) here is the answer of the developer himself
https:/ /github.com/Afrozaar/wp-api-v2-client-java/...
To solve the problem in my case, just install this plugin and activate it -
https://github.com/WP-API/Basic-Auth

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question