php code to call the dataverse API to upload files to existing dataset

136 views
Skip to first unread message

abella91

unread,
Apr 1, 2021, 5:11:28 AM4/1/21
to Dataverse Users Community
Hello,
 I'm trying to upload a file using php code to existing dataset by calling the api 
curl -H X-Dataverse-key:$API_TOKEN -X POST -F "file=@$FILENAME" -F 'jsonData={"description":"My description.","directoryLabel":"data/subdir1","categories":["Data"], "restrict":"false"}' "$SERVER_URL/api/datasets/:persistentId/add?persistentId=$PERSISTENT_ID,  but I'm getting error 400  bad request. Hereunder my php code

$ch = curl_init();
        if ($ch === false) {
            throw new Exception('failed to initialize');
        }
        $path='/path/to/file.txt'
        $data = array('file' => new \CURLFile(realpath( $path),  mime_content_type($path), basename($path)));
        $config = array(
            CURLOPT_URL => $url,
            CURLOPT_CONNECTTIMEOUT => 0,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SAFE_UPLOAD => true,
            CURLOPT_POSTFIELDS => $data ,
            CURLOPT_VERBOSE => true,
            CURLOPT_POST => TRUE,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array(
              'content-type: multipart/form-data'
          )
        );
        curl_setopt_array($ch, $config);
        $result = curl_exec($ch);
        curl_close($ch);

When i try to curl using terminal it is working file: 

in dataverse server, there are no logs showing more details about this 400 error 

can you help me to make my php code working 

Thanks


James Myers

unread,
Apr 1, 2021, 7:38:47 AM4/1/21
to dataverse...@googlegroups.com

I don’t see a jsonData entry in your php code. I’m not sure about how one would add that in php, but the api call requires both the file and the metadata in the jsonData object.

-- Jim

--
You received this message because you are subscribed to the Google Groups "Dataverse Users Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dataverse-commu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dataverse-community/927c0f19-9311-4dea-8473-0db3cc9c33e0n%40googlegroups.com.

Abdellah

unread,
Apr 1, 2021, 8:15:30 AM4/1/21
to dataverse...@googlegroups.com
@James, thanks for your response. I'm not sure that the parameter  jsonData is required since the curl command from the terminal(
curl -s -X POST -F "file=@/path/to/file.txt" 'https://dataverse-domain.fr/api/v1/datasets/:persistentId/add?persistentId=$persistentId&key=$userToken')
 is working without it



You received this message because you are subscribed to a topic in the Google Groups "Dataverse Users Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dataverse-community/iltyP_KGNMo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dataverse-commu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dataverse-community/MN2PR07MB734330A8A8B20789345CC22DBF7B9%40MN2PR07MB7343.namprd07.prod.outlook.com.

James Myers

unread,
Apr 1, 2021, 8:41:12 AM4/1/21
to dataverse...@googlegroups.com

Sorry for the false lead. The only other thing I can think of – looking at the code, I don’t see logging on the 400 errors but the response with those errors could have info that would help in diagnosing.

 

-- Jim

Abdellah

unread,
Apr 1, 2021, 8:52:51 AM4/1/21
to dataverse...@googlegroups.com
when I print the response I can see {\"status\":\"ERROR\",\"code\":400,\"message\":\"Bad Request. The API request cannot be completed with the parameters supplied. Please check your code for typos, or consult our API guide at http://guides.dataverse.org requestUrl\":\"https://dataverse-domain.fr/api/v1/datasets/:persistentId/add?persistentId=$persistentId &key=$myTokenValue\",\"requestMethod\":\"POST\"}. 

Stefan Kasberger

unread,
Apr 1, 2021, 10:34:01 AM4/1/21
to Dataverse Users Community
Could it be, that the persistentId string has a whitespace at the end? the request url in the response message has one between "$persistentId" and "&key".

abella91

unread,
Apr 1, 2021, 11:08:46 AM4/1/21
to Dataverse Users Community

the whitespace is just typo when i copied to this conversation, because i replaced the actual value with this variable $persistentId" . 

kiru...@gmail.com

unread,
Apr 1, 2021, 11:12:35 AM4/1/21
to Dataverse Users Community

abella91

unread,
Apr 1, 2021, 11:20:28 AM4/1/21
to Dataverse Users Community
@Péter, I'm sorry. I forget to put the ; in this conversation. if you forget that in the php code it will not be executed correctely and the request will not be sent to dataverse and it is not my case.

what i'm searching here is if someone already did this api call using php client, because in official doc there is only a Python example 

Péter Király

unread,
Apr 1, 2021, 11:40:41 AM4/1/21
to dataverse...@googlegroups.com
Hi,

I did not tried using API with PHP. Another hint:
php -l script.php
reports if there is any syntactical error in your script.

I do not know by heart, but you can set a curl option to give you more
details about the HTTP communication.

Best,
Péter

abella91 <bellahcen...@gmail.com> ezt írta (időpont: 2021. ápr.
1., Cs, 17:20):
> To view this discussion on the web visit https://groups.google.com/d/msgid/dataverse-community/d9037180-038e-4096-91c8-accfcdb8c7b9n%40googlegroups.com.



--
Péter Király
software developer
GWDG, Göttingen - Europeana - eXtensible Catalog - The Code4Lib Journal
http://linkedin.com/in/peterkiraly

Abdellah

unread,
Apr 1, 2021, 11:47:03 AM4/1/21
to dataverse...@googlegroups.com
for http logs i used  CURLOPT_VERBOSE => true

Philip Durbin

unread,
Apr 1, 2021, 2:00:53 PM4/1/21
to dataverse...@googlegroups.com
Hi Abdellah,

Please give the following a try. It works for me. You'll have to adjust the server URL, etc. of course.

#!/usr/bin/php
<?php

$dataset_pid='doi:10.5072/FK2/46EHCT';
$file_path='file.txt';
$base_url='http://localhost:8080';
$user_token='11289575-25b1-484d-8d8e-b4f437889e01';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$base_url/api/datasets/:persistentId/add?persistentId=$dataset_pid");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
    'file' => new \CURLFile(realpath($file_path), mime_content_type($file_path), basename($file_path)),
    'jsonData' => '{"description":"My description."}', // optional
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$headers = array(
    "X-Dataverse-Key: $user_token",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
print($result);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Thanks,

Phil



--

Abdellah

unread,
Apr 2, 2021, 4:48:41 AM4/2/21
to dataverse...@googlegroups.com
@phil, when i try your code i get 415 error {"status":"ERROR","code":415,"message":"HTTP 415 Unsupported Media Type"}
and when i add this header "content-type: multipart/form-data", i get the usual error 400

Reply all
Reply to author
Forward
0 new messages