Answer the question
In order to leave comments, you need to log in
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();
}
}
}
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]
implementation group: 'com.afrozaar.wordpress', name: 'wp-api-v2-client-java', version: '4.8.3'
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question