Skip to main content

I am using below code to upload pic to an item row but gets blank response, no error
Please help

$filepath = “dec-logo201901.png”;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://api.monday.com/v2/file’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
‘query’ => “mutation {
add_file_to_column (item_id: 6441733054, column_id: ‘files’, file: ‘$filepath’) {
id
}
}”,
‘variables’ => ‘"{"item_id": 6441733054, "column_id": "files"}"’,
‘map’ => ‘"{"image":"variables.file"}"’,
‘file’ => ‘@’ .realpath(“$filepath”)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$headers = array();
$headers = ‘Api-Version: 2024-01’;
$headers = 'Authorization: '.$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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

Hello there @rajnish123saini and welcome to the community!

I hope you like it here 💪

I have this example that might help you with your script:

import fs from 'fs';
import fetch from 'node-fetch';

// set filename
var upfile = 'images.png';

// set auth token and query
var API_KEY = "APITOKEN"
var query = 'mutation ($file: File!) { add_file_to_column (file: $file, item_id: 1122334455, column_id: "files") { id } }';

// set URL and boundary
var url = "https://api.monday.com/v2/file";
var boundary = "xxxxxxxxxx";
var data = "";

fs.readFile(upfile, function(err, content){

    // simple catch error
    if(err){
        console.error(err);
    }

    // construct query part
    data += "--" + boundary + "\\r\\n";
    data += "Content-Disposition: form-data; name=\\"query\\"; \\r\\n";
    data += "Content-Type:application/json\\r\\n\\r\\n";
    data += "\\r\\n" + query + "\\r\\n";

    // construct file part
    data += "--" + boundary + "\\r\\n";
    data += "Content-Disposition: form-data; name=\\"variables[file]\\"; filename=\\"" + upfile + "\\"\\r\\n";
    data += "Content-Type:application/octet-stream\\r\\n\\r\\n";
    var payload = Buffer.concat([
            Buffer.from(data, "utf8"),
            new Buffer.from(content, 'binary'),
            Buffer.from("\\r\\n--" + boundary + "--\\r\\n", "utf8"),
    ]);

    // construct request options
    var options = {
        method: 'post',
        headers: {
          "Content-Type": "multipart/form-data; boundary=" + boundary,
          "Authorization" : "APITOKEN"
        },
        body: payload,
    };

    // make request
    fetch(url, options)
      .then(res => res.json())
      .then(json => console.log(json));
});

Whats does this parameter means

$file: File!

Is this $file a simple var name?
What does File! mean does it represent some dynamic value or just a label to use as it is?

Can you do above code in PHP?


Hello again @rajnish123saini,

I do not have much experience with PHP but using an automatic “translator” from JS to PHP, I got this (again, please double-check it because it was done automatically):

<?php
// set filename
$upfile = 'images.png';

// set auth token and query
$API_KEY = "APITOKEN";
$query = 'mutation ($file: File!) { add_file_to_column (file: $file, item_id: 1122334455, column_id: "files") { id } }';

// set URL and boundary
$url = "https://api.monday.com/v2/file";
$boundary = "xxxxxxxxxx";
$data = "";

$content = file_get_contents($upfile);

// simple catch error
if ($content === false) {
    echo "Error: " . error_get_last()["message"] . "\\n";
    exit;
}

// construct query part
$data .= "--" . $boundary . "\\r\\n";
$data .= "Content-Disposition: form-data; name=\\"query\\"; \\r\\n";
$data .= "Content-Type:application/json\\r\\n\\r\\n";
$data .= "\\r\\n" . $query . "\\r\\n";

// construct file part
$data .= "--" . $boundary . "\\r\\n";
$data .= "Content-Disposition: form-data; name=\\"variables[file]\\"; filename=\\"" . $upfile . "\\"\\r\\n";
$data .= "Content-Type:application/octet-stream\\r\\n\\r\\n";
$payload = implode("", array(
    $data,
    $content,
    "\\r\\n--" . $boundary . "--\\r\\n"
));

// construct request options
$options = array(
    'http' => array(
        'method' => 'POST',
        'header' => array(
            "Content-Type: multipart/form-data; boundary=" . $boundary,
            "Authorization: " . $API_KEY
        ),
        'content' => $payload
    )
);

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

if ($response === false) {
    echo "Error: " . error_get_last()["message"] . "\\n";
} else {
    $json = json_decode($response, true);
    print_r($json);
}

$files is the name of the variable. Check the part that says // construct file part

File! is the type of data for that variable.

I hope that helps!

Cheers,
Matias