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.

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

1 Answer 1

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\" :
2
  • Yes, I have mention the node type in command : StringEntity se = new StringEntity("{ \"title\" : \"Sample post\" , \"type\" : \"article\" , \"body\" : { \"und\" :[{ \"value\" : \"comments about something\" }]} }"); Commented Sep 13, 2014 at 4:46
  • 1
    I test with Postman by command : {"type":"article","title":"test title"} and succesfull Commented Sep 13, 2014 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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