Hi @dipro, @Carlos-i, @Jean-DenisCaissie and others interested,
I experienced the same kind of errors Carlos is mentioning. Depending on the language / environment you are using you really need to find out how to exactly send a multipart request. I am using WordPress/PHP and sending a multipart request can only be done when you build the body of the request by concatenating strings. This is because you need to define a boundary in the header and use the boundary (preceded by ‘–’) in the body. The boundary in my case is a WordPress generated password, but it can be any unique string. Make sure it is really unique because you will run into problems when this string is part of the file contents.
In this piece of code (WordPress/PHP) you see how I use “normal queries” - here to create a new item with the name of the file as it’s item name. When I have the item_id I will then upload the file to the file column and as you can see the body needs to be build by concatenating the correct strings.
Don’t forget to determine the MIME type as monday’s file viewer needs to know. Also you need to make sure that there is an empty line in the body before the actual query and the contents of the file to be uploaded. If you omit these you will get a 500 error returned. See also the comments in the code.
In my WordPress environment MONDAY_TEAM and MONDAY_URL are constants defined in my wp-config file. My secret monday key is stored in the user’s profile field and retrieved by get_field(‘mondaykey’, ‘user_42’). You need to replace those variables with your own to be able to test this yourself.
The code is just the beginning of a larger project, so the full path of the file (on my WordPress server) and the board_id are fixed values for now.
This is the result on the board.
Have fun!
//the board id will be derived from another piece of code, just for now it is fixed
$boardid = ‘515108235’;
//full filepath will be derived from another piece of code, just for now it is fixed
$filepath = ‘/var/www/vhosts/101/292674/webspace/siteapps/WordPress-515402/htdocs/wp-content/uploads/monday/How to use Excellent Monday - 0.8.pdf’;
//fill fparts with [basename] and [filename] and determine the MIME type
$fparts = pathinfo($filepath);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fmime = finfo_file($finfo, $filepath);
//create a new item in the board
$header = array(
‘User-Agent’ => MONDAY_TEAM . ’ GraphQL Client’,
‘Authorization’ => get_field(‘mondaykey’, ‘user_42’),
‘Content-Type’ => ‘multipart/form-data’
);
$body = array(
‘query’ => 'mutation {create_item (board_id: ’ . $boardid . ‘, group_id: “topics”, item_name: "’ . $fparts[‘filename’] . ‘") {id}}’
);
$response = wp_remote_post( MONDAY_URL, [‘headers’ => $header, ‘body’ => $body] );
$arr1 = json_decode($response[‘body’], true);
$itemid = $arr1[‘data’][‘create_item’][‘id’];
//generate a boundary, needed for the Header (Content-Type) and the Body (to split in multiple parts)
$boundary = wp_generate_password(24);
$eol = “\r\n”;
$header = array(
‘User-Agent’ => MONDAY_TEAM . ’ GraphQL Client’,
‘Authorization’ => get_field(‘mondaykey’, ‘user_42’),
‘Content-Type’ => ‘multipart/form-data; boundary=’ . $boundary //mind the boundary here
);
//we need to build the body from scratch because it is multipart with boundaries
//take care of the extra empty line (twice $eol) before the actual query and file, otherwise an error 500 is returned
$body = ‘–’ . $boundary . $eol;
$body .= ‘Content-Disposition: form-data; name=“query”’ . $eol . $eol;
$body .= 'mutation ($file: File!) {add_file_to_column (item_id: ’ . $itemid . ‘, column_id: file, file: $file) {id}}’ . $eol;
$body .= ‘–’ . $boundary . $eol;
$body .= ‘Content-Disposition: form-data; name=“variables[file]”; filename="’ . $fparts[‘basename’] . ‘"’ . $eol;
$body .= 'Content-Type: ’ . $fmime . $eol . $eol;
$body .= file_get_contents($filepath);
$body .= $eol . ‘–’ . $boundary . ‘–’;
$response = wp_remote_post( MONDAY_URL, [‘headers’ => $header, ‘body’ => $body] );