2

Using Content Type: application/json I tried like:

EDIT:

{
 "type": "article",
 "title": "Test article with image1",
 "body": {
 "und": [{
     "value": "test body!"
   }]
  },
 "field_image":{
 "und":[{
  "fid":{
  "filename": "bar.jpg",
  "target_uri": "sites/default/files/styles/large/public/field/image/bar.jpg",
  "filemime": "image/jpeg",
  "file":        "iVBORw0KGgoAAAANSUhEUgAAAKQAAAFzBAMAAABC179RAAAAD1BMVEUYooorZrPZwVO3Sd0M9BOSfwC2VwFIl7c9aAAAAABJRU5ErkJggg==",
   "filesize": "12345"
       }
     }]
    },

  "language": "und"
 }

Problem: Image is not created.

0

You need to use the file resource to upload the file first; that will provide you with a fid (file ID), which gets attached to the field on the node. The payload for that request might look something like:

{
    "filename": "foo.jpg",
    "target_uri": "bar/foo.jpg",
    "filemime": "image/jpeg",
    "file": "iVBORw0KGgoAAAANSUhEUgAAAKQAAAFzBAMAAABC179RAAAAD",
    "filesize": "12345"
}

And then to attach it the field your existing code will change to something like:

"field_image":{
  "und": [{
    "fid": [FILE ID RECEIVED FROM FILE SERVICE HERE]
   }],
}
  • Should I post first part first,so that I can get fid,again I have to post the second one with fid,what is target uri?is it like /site/default/files/images? – Hiranya Sarma Jan 29 '16 at 11:10
  • Yeah exactly, but it uses the public schema uri instead of an absolute path (the public:// bit). Might be worth experimenting with just making a single call and using the first object in place of the object with "fid" in it - services might be clever enough to create the file for you when it's attached to a node, I'm not sure – Clive Jan 29 '16 at 11:18
  • Posting first part I got response like this {"fid":"13","uri":"http://www.drupal1.com/formnode/file/13"} – Hiranya Sarma Jan 29 '16 at 11:20
  • Yep that's spot on, you can use the fid to attach the file to the node now. But I would try the one-request approach too just in case – Clive Jan 29 '16 at 11:24
  • I updated my code. – Hiranya Sarma Jan 29 '16 at 11:27

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.