Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
You have to specify uri in JSON payload of POST
You have to specify uri in JSON payload of POST
Comments
Comment #2
Wim LeersYou can't yet, we're fixing that in #1927648: Allow creation of file entities from binary data via REST requests.
Comment #4
pranilkochar CreditAttribution: pranilkochar commentedhii, im using DRUPAL 8.1.8 but not able to post a file using rest api
Comment #5
pranilkochar CreditAttribution: pranilkochar commentedmy 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?
Comment #6
HAMMAMI_IMED CreditAttribution: HAMMAMI_IMED commentedthe same problem.im using DRUPAL 8.2.0 but not able to post a file using rest api. I need help
Comment #7
shruthiganesh CreditAttribution: shruthiganesh as a volunteer commentedhref to upload image is "http://localhost/rest/type/file/image"
Comment #8
a.gursoy CreditAttribution: a.gursoy commented[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
RETURNS
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.
Comment #9
vjsutar CreditAttribution: vjsutar commentedFor Drupal 8.3.x
POST: authentication: basic_authentication, formats: hal_json, json
URL:
Header:
Method:
Body:
Response:
Comment #10
fallcool CreditAttribution: fallcool commentedI followed the steps at #9. An error was received: "Field _links is unknown". I am using Drupal 8.3.2.
Comment #11
moxihang CreditAttribution: moxihang commented@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.
Comment #12
humPhin CreditAttribution: humPhin commentedsame issue as #11
Comment #13
rob3000 CreditAttribution: rob3000 commentedThere is a patch for the File Entity module that will allow the file to be saved permanently here: https://www.drupal.org/node/2756127
Comment #14
garglalit0 CreditAttribution: garglalit0 commentedhi 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
Comment #15
marctty CreditAttribution: marctty commentedHow 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.
Comment #16
yogesh.shukla CreditAttribution: yogesh.shukla commentedHi 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
Comment #17
pranav.aeer CreditAttribution: pranav.aeer commentedHi All,
Are there any inputs on #16, please share if any.
Comment #18
stingray CreditAttribution: stingray commentedFinally, 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:
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?
Comment #19
stingray CreditAttribution: stingray commentedFound an answer to my question: "destination directory not defined".
You have to specify uri in JSON payload of POST:
Now file deleverd and stored unter /sites/default/files directory
Comment #20
AntonioPC CreditAttribution: AntonioPC commentedI 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.
I get the following error.
I cannot understand what I am doing wrong.
Can anybody help me?
Comment #21
apaderno@AntonioPC This support request has been closed 5 years ago. Nobody will post an answer here.
Comment #22
Saurabh.123 CreditAttribution: Saurabh.123 commented@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 :
Can you at least provide a link so that we can solve this issue?
Comment #23
apaderno@Saurabh.123 The forums are a better place to ask similar questions.
Comment #24
magendiran CreditAttribution: magendiran commentedThanks @vjsutar,
I have followed the steps #9 in my Drupal 9.5.1 and it is working fine.