I am trying to use AsyncHttpClient to make a JSON request. I tried to use several implementations, including setBody and addBodyPart, and many more. The result is always the same.
It should send a string like this converted to bytes:
{"template":"c=KZ;o=Template;cn=GOST_RAUTIL_USER_1Y;","useHSM":"true","altAttrs":{"altDN":["CN=Test org1, SERIALNUMBER=1111111111","CN=Test org2, SERIALNUMBER=2222222222"]},"keyName":"XkjCPFLfvJCUYktH","dn":"C=KZ, O=TEST, CN= XkjCPFLfvJCUYktH","internalHSMkey":"true"}
But somehow it converts the request into this:
..an ip address.. POST headers: Content-Type:application/json Authorization:Basic SWdvcjpHYW1tYTIwMTk=
So the server gets a wrong request and responses with an error meessage:
A JSONObject text must begin with '{' at 0 [character 1 line 1] json:
Here is one of my code implementations:
String req = jsonObject.toString();
byte[] req1 = req.getBytes();
ByteArrayPart request1 = new ByteArrayPart(req, req1);
Request request = new RequestBuilder(POST)
.setUrl(apiUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()))
.addBodyPart(request1)
.build();
asyncHttpClient.executeRequest(request, new AsyncCompletionHandlerBase() {
@Override
public void onThrowable(Throwable t) {
}
@Override
public org.asynchttpclient.Response onCompleted(org.asynchttpclient.Response response) throws Exception {
System.out.println("COMPLETED");
return super.onCompleted(response);
}
});
I got an answer by slandelle on GitHub:
Parts are for multipart encoding. You must set a full body.
Request request = new RequestBuilder(POST)
.setUrl(apiUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()))
.setBody(jsonObject.toString().getBytes())
.build();
--
You received this message because you are subscribed to the Google Groups "asynchttpclient" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asynchttpclie...@googlegroups.com.
To post to this group, send email to asyncht...@googlegroups.com.
Visit this group at https://groups.google.com/group/asynchttpclient.
To view this discussion on the web visit https://groups.google.com/d/msgid/asynchttpclient/8fdd2bff-b1e4-4e54-8ef3-49f9422d6db6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to asyncht...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to asynchttpclie...@googlegroups.com.
To post to this group, send email to asyncht...@googlegroups.com.
Visit this group at https://groups.google.com/group/asynchttpclient.
To view this discussion on the web visit https://groups.google.com/d/msgid/asynchttpclient/0dd95a10-7c29-440f-af3c-d0093fa55bd5%40googlegroups.com.