Content-Type | Techbirds

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 = “https://jsondatatest.com”;                                                                                            //Your webservice URL
String urlParameters = “{ ”email”: ”[email protected]”, ”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”: ”[email protected]”, ”password”: ”test123”}”);            // Your JSON raw data

QUrl url(“https://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()