How to post raw JSON Data in Webservices (JAVA, C++)

Hi All,

As we all know there are various ways of  passing data in request  of a web-service. Here I am going to discuss how I did for sending raw json data using post method.

You need to consider two important aspects of Request Header of request:-

Content-Type :-The MIME type of the body of the request e.g. “application/json”

Content-Length:-The length of the request body ( length of the raw json data in terms of number of characters).

JAVA

String targetURL = “http://jsondatatest.com”;                                                                                            //Your webservice URL
String urlParameters = “{ \”email\”: \”swati@y.com\”, \”password\”: \”test123\”}”;                 // Your JSON raw data

URL url = new URL(targetURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();                    //Creating connection object

connection.setRequestMethod(“POST”);                                                                                                      //Way of submitting data(e.g. GET, POST)
connection.setRequestProperty(“Content-Type”, “application/json”);                   //Setting content type-  JSON

//Setting length of the request body
connection.setRequestProperty(“Content-Length”, Integer.toString(urlParameters.getBytes().length));  

//set it to true if you want to send input in the request body
connection.setDoInput(true);

//set it to true if you want to send outputin the request body
connection.setDoOutput(true);

 

//Sending raw json data in the request body

DataOutputStream dataoutput = new DataOutputStream(connection.getOutputStream());
dataoutput .write(urlParameters.getBytes(“UTF-8″));
dataoutput .flush();

//Retrieving response from the webservice
BufferedReader bufferResponse = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuffer response = new StringBuffer();
while ((line = bufferResponse .readLine()) != null) {
response.append(line);
response.append(‘\r’);
}

//Process your response as needed
process(response);

//Freeing resources
bufferResponse .close();

*********************************************************************************************************************

C++ (Used Qt framework )

//QNetworkAccessManager - Receives network requests, and sends them over the network using one of its HTTP request functions

QNetworkAccessManager *manager = new QNetworkAccessManager(this);

QByteArray dataByteArray(“{ \”email\”: \”swati@y.com\”, \”password\”: \”test123\”}”);            // Your JSON raw data

QUrl url(“http://jsondatatest.com”);                                                                                                                    //Your webservice URL

QNetworkRequest request(url);
QByteArray postDataSize = QByteArray::number(dataByteArray.size());

request.setRawHeader(“User-Agent”, “ApplicationNameV01″);
request.setRawHeader(“Content-Type”, “application/json”);
request.setRawHeader(“Content-Length”, postDataSize);

if (manager) {
bool result;

//Used QEventLoop to make the webservice call synchronous, if you want it to be asynchronous avoid it.
QEventLoop loop;
QNetworkReply *reply = manager->post(request, dataByteArray);

/* The finished() signal is triggered once an HTTP request is complete, by connect method we are connecting finished signal with the slot method onRequestFinished(user defined method). If it returns true it means connection is successful else not. You can process the response of the call in this user defined method */

result = connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(onRequestFinished(QNetworkReply*)));
loop.exec();

qDebug() << “Connection is success : ? : ” << result;
if (reply) {
qDebug() << “Reply from server is ” << reply;
}

 

This is all from my side. Please keep commenting in case of any doubt or correction :)

6,211 total views, 1 views today

Share this OnShare on FacebookTweet about this on TwitterShare on LinkedInShare on Google+

Leave a Reply