2

I've got a content type with "File" field and "File" widget. I am successfully able to create a node without a file using restws. But, if I want to attach a file to it, it just doesn't allow me to do that. I've tried many different things, but with failure.

Also, when accessing a node with file already attached to it, using get request (e.g. {domain_name}/node/219.json), the file field is displayed as an empty element, even though file is existing and is displayed fine when viewing the node in edit mode from admin area.

E.g.

JSON:

"field_simple_file":[]

XML:

<field_simple_file/>

The node array without file field, that succeeds is as below:

$new_node = array(
    'body' => array(),
    'type' => 'simple',
    'title' => 'Content created with restws',
    'language' => 'und',
    'status' => 1,
    'promote' => 0,
    'sticky' => 0,
    'author' => 1    
);

And content-type:

'Content-Type: application/json'

But I think, I am not 100% sure on how to include a file in it (have tried few different things), which might be leading to failure. Can anyone provide me with guidance for attaching/uploading a file with restws please?

1

Here is the URL to access a node with file already attached to it

{domain_name}/[end-point]/node/219.json

Here 219 is the nid of the node.

Here is the sample JSON format to attach a file to node (node create).

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

I created (and I am maintaining) the RESTful web services support for files and images module, which I believe does exactly what you were asking for.

This module provides support for creating and updating fields of type file and image. It accepts (multiple) image/file data as base64_encoded strings in the JSON payload.

It requires the RESTful Web Services module.

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.