Kong Kong - 1 year ago 149
reST (reStructuredText) Question

Android HttpURLConnection, POST: Error message, Node type is required

I have problem posting content to my Drupal site from Android.

I get the error message:

Node type is required


I have tested the url in Postman. And it works. I am able to add new articles. Its just on Android that i have this error.

I have a question. Do I need to add the session id and session name, to make a post? If so, can you please give an example of how this is achieved.
This is my first time making a post request. Not sure how much of the code is correct. I had trouble finding a good tutorial for beginners.

In Manifest I have


I have saved my token, session id and session name.

public String session_name;
public String session_id;
public String token;

Posting of values

protected String doInBackground(Void... voids) {
String address = "http://app.flickgo.com/apistuff/node.json";
HttpURLConnection urlConnection;
try {
URL url = new URL(address);
urlConnection = (HttpURLConnection) url.openConnection();
String userCredentials = "My_Username:My_Password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
urlConnection.setRequestProperty ("Authorization", basicAuth);
urlConnection.setRequestProperty("X-CSRF-Token", token);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));

//i want to save the title of an article
String title = "Mobile";
String input = ("{\"title\":\""+title+"\",\"type\":\"article\"}");


JSONObject jsonObject = new JSONObject();
InputStream inputStream;
// get stream
if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
// parse stream
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp, response = "";
while ((temp = bufferedReader.readLine()) != null) {
response += temp;
// put into JSONObject
jsonObject.put("Content", response);
jsonObject.put("Message", urlConnection.getResponseMessage());
jsonObject.put("Length", urlConnection.getContentLength());
jsonObject.put("Type", urlConnection.getContentType());

return jsonObject.toString();
} catch (IOException | JSONException e) {
return e.toString();

Answer Source

I solve the issue by using this code.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://yoursite/endpoint/node");  
try {
            //get title and body UI elements
            TextView txtTitle = (TextView) findViewById(R.id.editTitle);
            TextView txtBody = (TextView) findViewById(R.id.editBody);

            //extract text from UI elements and remove extra spaces
            title= txtTitle.getText().toString().trim();

            // set json to StringEntity

            StringEntity se = new StringEntity( " { \"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}");
            //  set httpPost Entity

            // Set some headers to inform server about the type of the content
            httppost.setHeader("Content-type", "application/json");
            httppost.setHeader("X-CSRF-Token", session_token);

            // 8. Execute POST request to the given URL
            HttpResponse response = httpclient.execute(httppost);
            Log.v("Response from Drupal: ", EntityUtils.toString(response.getEntity()));

            return 0;

        }catch (Exception e) {
            Log.v("Error adding article",e.getMessage());