You have to specify uri in JSON payload of POST

Comments

heykarthikwithu created an issue. See original summary.

Wim Leers’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

pranilkochar’s picture

hii, im using DRUPAL 8.1.8 but not able to post a file using rest api

pranilkochar’s picture

my url=http://localhost/entity/file

request body:

{"_links":
{
"type":{"href":"http://localhost/rest/type/file/file"},
},
"filename":[{"value":"pranil.jpg"}],
"filemime":[{"value":"image/jpeg"}],
"data":[{"value":"base64 content"}] }

error:
{
"message": "A fatal error occurred: cURL error 6: Could not resolve host: http (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)"
}

any suggestions?

HAMMAMI_IMED’s picture

the same problem.im using DRUPAL 8.2.0 but not able to post a file using rest api. I need help

shruthiganesh’s picture

href to upload image is "http://localhost/rest/type/file/image"

a.gursoy’s picture

[Note: I found solution myself. Check below]

I tried to change href as described #7 but result is same :/

POST

drupal8.loc/entity/file?_format=hal_json

BODY

{
"_links": {
"type": {
  "href": "http://drupal8.loc/rest/type/file/image"
}
},
"filename": [
{
  "value": "favicon-32x32.png"
}
],
"filemime": [
{
  "value": "image/png"
}
],
"data": [
{
  "value": "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAkUExURUxpcSOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4U0m8UcAAAAMdFJOUwD/EvAm2qhTPMRrkl4CMYAAAAE1SURBVCjPXZG/S8NQEMePR7Ehnb48khC7BEFFnYJD6/aWOLilVBy6WBVc648/oCii1KlFKDrFwV2tqH+edy9Rk7zh8e7Dfe++944oP5dUO59RDQyPqrFK/WqKCzxUwA30oJyymervlVK8PsVBOd85hh6pyflfhwXg024cjguwBqCjhsCsSOAn+k4MBGpUWAB6Tb6W1VxAgv+MfcPgWUBgazjxoYwlQLrsmBa6MhbH3hep2yu6QJuowWL9Yfs1U4QRSbWOjRts0DMCdEbqjl8s1kYkPqmX7YGRfgy4aJe2IB+0wZJI2s7ojEGbWtJejL1aL55h+wGDVWTWix47+cAug6kAycjs+LkkjFzku0h6dC3D01KxCmdOLrt7pyQ0+Z/tRXT/eEr0dvL7y6zkrbhPtYVPrOAHKZQ53IyepQMAAAAASUVORK5CYII="
}
]
}

RETURNS

The website encountered an unexpected error. Please try again later.

GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: http (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in GuzzleHttp\Handler\CurlFactory::createRejection() (line 186 of vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php).

SOLVED

1- I installed File Entity module: https://www.drupal.org/project/file_entity
2- I assign administration role to my user which is I request services with it's basic auth info.

And problem solved.

vjsutar’s picture

For Drupal 8.3.x

  • Enable File resource from admin/config/services/rest
  • POST: authentication: basic_authentication, formats: hal_json, json

  • Download file_entity module and enable it.
  • Set permission Add and upload new files under file entity.
  • REST Request Example:
  • URL:

{{Domain}}/entity/file?_format=hal_json

Header:

Authorization:Basic <token>
Content-Type:application/hal+json

Method:

POST

Body:

{
	"_links": {
		"type": {
			"href": "{{Domain}}/rest/type/file/image"
		}
	},
	"filename": [
		{
			"value": "imagename.jpg"
		}
	],
	"data": [
		{
			"value": "base64encoded image string"
		}
	]
}

Response:

{
  "_links": {
    "self": {
      "href": "{{Domain}}/file/45?_format=hal_json"
    },
    "type": {
      "href": "{{Domain}}/rest/type/file/image"
    }
  },
  "fid": [
    {
      "value": "45"
    }
  ],
  "uuid": [
    {
      "value": "d49af3a3-088b-4934-8cea-860660d9b776"
    }
  ],
  "langcode": [
    {
      "value": "en"
    }
  ],
  "type": [
    {
      "target_id": "image"
    }
  ],
  "filename": [
    {
      "value": "imagename.jpg"
    }
  ],
  "uri": [
    {
      "value": "public://file906cnk"
    }
  ],
  "filemime": [
    {
      "value": "application/octet-stream"
    }
  ],
  "filesize": [
    {
      "value": 51672
    }
  ],
  "status": [
    {
      "value": false
    }
  ],
  "created": [
    {
      "value": 1488441009
    }
  ],
  "changed": [
    {
      "value": 1488441009
    }
  ],
  "data": [
    {
      "value": "base64value"
    }
  ]
}
fallcool’s picture

I followed the steps at #9. An error was received: "Field _links is unknown". I am using Drupal 8.3.2.

moxihang’s picture

@vijaysutar2050 i use you way to creat image file,it success but the file status is temp,not create image file ,and i can't use it at node create.

humPhin’s picture

same issue as #11

rob3000’s picture

There is a patch for the File Entity module that will allow the file to be saved permanently here: https://www.drupal.org/node/2756127

garglalit0’s picture

hi i am trying to post image from api but the file is not copied it is creating .tmp file i.e file1.tmp something like that my orginal file is not copied to the druapl fo;lder location please help me if anyone has solution

marctty’s picture

How procede for private files managed by drupal.
Using this api with private configuration compairs file node with broken attachment. The files on other hand are correctly trasferred to the server web. Have i to add extra parameters to the post?
The instruction for a public configuration run correctly crating a node with accessible file.

yogesh.shukla’s picture

Hi All,
I am implementing react js in front and Drupal as backend, Now Suppose I am creating article type node, I am taking form input, then passing POST type request to drupal and creating article everything is working good, now I want to post image as well for the article with the same request that being sent for article creation, How can send image with rest fields like title, body and so on.
like react code something below:

let config_header = {
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': 'drW-ozUVkMAroWqk5iqd3aoazC2wYnmKp3TP66jIyRY',
'Authorization': 'Basic YWRtaW46YWRtaW4=',
}
}

const article = {
"type": [
{
"target_id": "article"
}
]
,
"title": [
{
"value":this.state.article_title,
}
],
"body": [
{
"value":this.state.article_body,
}
]
};

above I want to add image field as well with the same request
how can do that

pranav.aeer’s picture

Hi All,
Are there any inputs on #16, please share if any.

stingray’s picture

Version: 8.1.x-dev » 8.9.x-dev
Issue summary: View changes

Finally, I managed to deliver the file to the server, but then the error 500 rased due destination folder not set. I can't figure where to define this destination directory: in request JSON data or header, or somewhere else in the settings.php or somewhere in REST configuration?

Edited this source core/lib/Drupal/Core/File/FileSystem.php line 488 in order to print the $destination, and it appears to be empty. See '' in the server log below:

H01071: Got error 'PHP message: Uncaught PHP Exception Drupal\\Core\\File\\Exception
\\DirectoryNotReadyException: "The specified file 'temporary://filemDIAwD' could not be copied 
because the destination '' directory is not properly configured. This may be caused by a problem 
with file or directory permissions." at /var/www/vhosts/web.site/core/lib/Drupal/Core/File
/FileSystem.php line 488'

Tried to define 'file_private_path' folder in settings.php, added 'tmp' folder under 'sites/default/files' but with no result. What am I missing here?

stingray’s picture

Issue summary: View changes
Issue tags: +json, +image upload, +REST, +REST API, +restful

Found an answer to my question: "destination directory not defined".

You have to specify uri in JSON payload of POST:

"uri": [
            {
                "value": "public://" + media_name + ".jpg"
            }
        ],

Now file deleverd and stored unter /sites/default/files directory

AntonioPC’s picture

I am trying to insert a image in Drupal from Postman.

The request URL is

http://<urldrupal>/drupal/node?_format=hal_json.

The JSON structure inserted in the body is the following.

{
  "url": "http://<urldrupal>/drupal/rest/type/file/image",
  "type": "POST",
  "data": {
    "_links": {"type": {"href": "http://<urldrupal>/drupal/rest/type/file/image"}},
    "filename": [{"value": "test.jpg"}],
    "filemime": [{"value": "image / jpeg"}],
    "type": [{"target_id": "image"}],
    "data": [{"value": "BASE64_VERSION_OF_THE_IMAGE_HERE"}]
  }
}

I get the following error.

{
    "message": "The type link relation must be specified."
}

I cannot understand what I am doing wrong.

Can anybody help me?

apaderno’s picture

Version: 8.9.x-dev » 8.1.x-dev
Issue tags: -json, -image upload, -REST, -REST API, -restful

@AntonioPC This support request has been closed 5 years ago. Nobody will post an answer here.

Saurabh.123’s picture

@kiamlaluno how can we get the answer to the question asked by @AntonioPC
As I am facing the same issue. I am getting the following error by running the above API :

{
    "message": "Could not determine entity type bundle: \"type\" field is missing."
}

Can you at least provide a link so that we can solve this issue?

apaderno’s picture

@Saurabh.123 The forums are a better place to ask similar questions.

magendiran’s picture

Thanks @vjsutar,
I have followed the steps #9 in my Drupal 9.5.1 and it is working fine.