Send a JSON

80 views
Skip to first unread message

Игорь Хмелевский

unread,
Jul 1, 2019, 1:59:55 AM7/1/19
to asynchttpclient
 

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.

Well, I tried to do that as well:
Request request = new RequestBuilder(POST)
.setUrl(apiUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()))
.setBody(jsonObject.toString().getBytes())
.build();
 
The result is the same. It just sends this line
POST headers: Content-Type:application/json Authorization:Basic SWdvcjpHYW1tYTIwMTk=
instead of 
{"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"}

slan...@gatling.io

unread,
Jul 1, 2019, 2:36:02 AM7/1/19
to asyncht...@googlegroups.com
AHC doesn’t log binary data, it doesn’t mean it’s not sent.
--
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.

Игорь Хмелевский

unread,
Jul 1, 2019, 2:46:53 AM7/1/19
to asynchttpclient
Alright then. 
Still, it says that JSONObject is wrong, but if I send it with a common httpURLConnection - it works fine.
And can't find out why it happens and how to solve that.
How can I see what AHC really sends if it doesn't log the data?

понедельник, 1 июля 2019 г., 12:36:02 UTC+6 пользователь Stéphane LANDELLE написал:
To unsubscribe from this group and stop receiving emails from it, send an email to asyncht...@googlegroups.com.

slan...@gatling.io

unread,
Jul 1, 2019, 2:49:54 AM7/1/19
to asyncht...@googlegroups.com
Why do you insist on sending a binary payload if what you have is a String?
If server says your payload is malformed, it most likely mean you do have a typo.
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.

Игорь Хмелевский

unread,
Jul 1, 2019, 2:54:36 AM7/1/19
to asynchttpclient
See, the server gets data in bytes. So I send the data in bytes. 
Nevertheless, I tried to send it as String, like this .setBody(jsonObject.toString())
but it gives the same result.
And if I send the same jsonObject via httpURLConnection using OutputStream, it works well. So I think that there is no a typo in this jsonObject. I suppose it converts somewher in a wrong way.)



понедельник, 1 июля 2019 г., 12:49:54 UTC+6 пользователь Stéphane LANDELLE написал:
Reply all
Reply to author
Forward
0 new messages