1

I am trying to create a new node using the restws drupal module. My content type has just two fields: title and a file. However, when i send the request with following payload, i get an error. The file which i am referring in the payload is already existing on my drupal server.

URL : CMS_BASE_URL/node Method : POST

Payload:

<?xml version="1.0" encoding="utf-8"?>
<data>
      <field_my_file>
                <file resource="file" id="24">CMS_BASE_URL/file/24</file>
      </field_my_file>
      <type>filetest</type>
      <title>My 1st file test</title>
      <language>en</language>
      <author resource="user" id="5">
      </author>
</data>

Error - 406 Not Acceptable: Invalid data value given. Be sure it matches the required data type and format.

Is there any problem in the way i am sending the payload? If i remove the "file" part from the payload, it is able to create node.Please guide me if this is not correct.

2 Answers 2

0

Since the RESTful WS module internally uses the Entity APIs to create the content, the problem here is with the format of the data that are passed. While creating the content programmatically with the Entity API and attaching any files, you need to pass the file parameters in the below format:

// omitted rest of the code for brevity
$node->field_image[LANGUAGE_NONE][] = array(
  'fid' => $file->fid,
  'display' => 1,
  'description' => '',
);
node_save( $node );

Having said that, the solution to your problem would be the data in the below format:

<field_my_file>
    <fid>24</fid>
    <display>1</display>
    <description></description>
</field_my_file>
<type>filetest</type>
<title>My 1st file test</title>
<language>en</language>
<author resource="user" id="5">
</author>

And the equivalent JSON would be:

{
    "type": "filetest",
    "title": "My 1st file test",
    "language": "en",
    "author": {
        "resource": "user",
        "id": "5"
    },
    "field_my_file": {
        "fid": "24",
        "display": 1, // required to avoid integrity constraints voilation
        "description": "" // you may choose to omit this field
    }
}
2
  • How does it different from answer posted Jun 27 at 13:35 by Krishna Mohan?
    – Mołot
    Commented Sep 9, 2014 at 7:13
  • @Mołot: Observe the structure changes between the two, the actual problem mentioned here was with the linking of the existing file, and in that case you will need to pass the data in the mentioned format for it to get linked with the newly created node.
    – eMAD
    Commented Sep 9, 2014 at 8:45
0

I tested the following code it works fine. Replace your field machine names and bundle name accordingly. Hope it helps

Here is the XML format to send data

<result>
<title>title_of_node</title>
<type>sampleimage</type>
<field_samplefile>
<und is_array="true">
<item>
<fid>18</fid>
<uid>1</uid>
<filename>Drupal.txt</filename>
<uri>public://Drupal.txt</uri>
<filemime>text/plain</filemime>
</item>
</und>
</field_samplefile>
</result>

Here is the JSON format

{
    "title": "title_of_node",
    "type": "bundle_name",
    "field_samplefile": {
        "und": [{
            "fid": "17",
            "filename": "changes.txt",
            "uri": "public://changes.txt",
            "filemime": "text/plain"
        }]
    } 
}

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.