1

I use restful between Drupal 7 and Android app. I logged in successful and got 3 variable session_id, session_name and token When I use POST method to create node. I getting error : "node type is required" This is my code :

private class addArticleTask extends AsyncTask<String, Integer, Integer> {

    protected Integer doInBackground(String... params) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://mysite/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
            JSONObject json = new JSONObject();
            String title=txtTitle.getText().toString().trim();
            String body=txtBody.getText().toString().trim();


            //add raw json to be sent along with the HTTP POST request
            StringEntity se = new StringEntity("{ \"title\" : \"Sample post\" , \"type\" : \"article\" , \"body\" : { \"und\" :[{ \"value\" : \"comments about something\" }]} }");
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            se.setContentType(new BasicHeader("Cookie",session_name+"="+session_id));
            se.setContentType(new BasicHeader("X-CSRF-Token",token));
            httppost.setEntity(se);


            HttpResponse response = httpclient.execute(httppost);
            Log.v("Return add article", EntityUtils.toString(response.getEntity()));


            return 0;

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

        return 0;
    }

In Logcat : Return add article : ["node type is required"] any one help me.

  • I think your post URL should be http://mysite/endpoint/node.json – tyler.frankenstein Sep 13 '14 at 2:43
  • @tyler.frankenstein I edit URL and I still have this issue – user3721110 Sep 13 '14 at 3:03
  • hi. did you solved that error? – David Nov 22 '17 at 12:14
1

You must mention the node type while doing the POST.

node_type will the machine name of your content type

for example try creating page with this,

StringEntity se = new StringEntity("{ \"title\" : \"Sample post\" , \"page\" :
  • Yes, I have mention the node type in command : StringEntity se = new StringEntity("{ \"title\" : \"Sample post\" , \"type\" : \"article\" , \"body\" : { \"und\" :[{ \"value\" : \"comments about something\" }]} }"); – user3721110 Sep 13 '14 at 4:46
  • 1
    I test with Postman by command : {"type":"article","title":"test title"} and succesfull – user3721110 Sep 13 '14 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.