This question already has an answer here:

I have a remote front-end application under development and I would like to interface with Drupal as a backend service, using the RestWS module to create a RESTful service to my Drupal instance (using JSON as my data format).

I have a node content type of type "article", that has an image field on it called "field_image".

I'm currently trying to figure out the proper way to upload a file to Drupal through the API created by RestWS.

I can retrieve a node with a file attachment via GET to the URL http://www.example.com/node/1.json, which retrieves the contents of node 1, including the information about the uploaded file:

https://gist.github.com/lhridley/8bbf95294aa529d74c74

I can also retrieve information specifically about the file attachment, using the FID returned when requesting information about the node, using the url http://www.example.com/file/1.json:

https://gist.github.com/lhridley/804eb93016845f369535

Now, my question:

How specifically do you upload a file using RestWS, so that you create a file resource to then use when creating a node?

marked as duplicate by kiamlaluno Mar 10 '16 at 16:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

i use services not restWS, but the steps are quite similar on every REST service.

with your REST client. ie, restsharp, in my case im using Http (plugin for unity). your first need to be connected, to do that call your server.dominio.co/site/apiName/user/login.json (you need to tick the option on your rest api, that enables user login service). also need to tick file creation permission. if your are using token rememter to save it, also the session name, and session id myForm.AddHeader(session_name,session_id) (is the cookie). in my case i dont send it because http already do it for me. after that this is how i upload a file with c# and services,

Hashtable hashdata = new Hashtable();

    WWWForm myForm = new WWWForm();
    myForm.AddField("file", base64Img);
    myForm.AddField("filename", name);
    myForm.AddField("filepath", "public://" + folderName);
    string path = server.dominio.co/site/apiName + "file";
    HTTP.Request theRequest = new HTTP.Request("post", path, myForm);

    theRequest.synchronous = true;
    theRequest.AddHeader("X-CSRF-Token", myTOKEN);
    theRequest.Send((request) =>
    {
        if (request.response != null)
        {
            request.response.GetHeaders();

            string result = request.response.Text;
            hashdata = new Hashtable();
            hashdata = (Hashtable)JSON.JsonDecode(result);

        }

    });
    return hashdata;

will return the fid of the file just uploaded... after that you will need to update the field of the node where you want the file to be attached to... you will need a new REST request to update that node, method update, etc, etc

Hopes this helps a bit.... know it's not THE solution... but hope it points you the right way.

  • Thanks! Is the only options with RestWS to base64 encode the image file? – Lisa Ridley Apr 27 '15 at 17:12
  • mmm... never have tried raw data... I usually work with base64 (strings in general) to keep server layer and client layer independent... if i need another client to consume from the server i've everything as string – Facundo Chaer Apr 27 '15 at 18:37
  • 1
    The Services module has the option now to set up an endpoint specifically for a node with an image or file field, and take a POST as multipart/form-data instead of JSON, which gives you some ability to upload image and files and attach them to an existing node. Was hoping to avoid using both Services & RestWS due to overhead (RestWS is just so much cleaner in so many ways). I haven't gotten multiple images to post in an image field that takes multiple images, nor have I deleted & replaced an image yet. But, no base 64 encoding required, unless it's within the Services module code itself. – Lisa Ridley Apr 28 '15 at 16:04

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