Skip to main content

While sending multipart-form data POST request to BOX from ServiceNow, I am getting this error from the Box API:


{“code”:“bad_request”,“help_url”:“http://developers.box.com/docs/#errors",“status”:400,“message”:"API upload did not contain a file part”,“type”:“error”}



The payload which is being sent over on this request, looks something like this:



–xXxxxxxxxxxxxxxxxxXx"


,Content-Disposition:form-data; name=“attributes”,{“name”:“config.txt”,“parent”:{“id”:“0”}},–xXxxxxxxxxxxxxxxxxXx,Content-Type:text/plain,Content-Disposition:form-data; name=“file”; filename=“config.txt”


,mB@7e8de4,–xXxxxxxxxxxxxxxxxxXx–



Any idea of what I am doing wrong here?

Hi @pratyush_mp ,



Looks like you’re trying to upload a file, but I’m not sure.



Can you send us more details on the POST request?


Hi @rbarbosa ,



Yes I am trying to upload a file from ServiceNow using a POST Request.



The script used for sending this request is given below for your reference:



var boundary = ‘xXxxxxxxxxxxxxxxxxXx’;



var attach = new GlideRecord(‘sys_attachment’);


attach.addQuery(‘table_sys_id’, ‘<sys_id>’);


attach.query();



if (attach.next()) {



try {

var sa = new GlideSysAttachment().getBytes(attach);



var file = {

name: attach.getValue('file_name'),

mimeType: attach.getValue('content_type'),

contentTransferEncoding: 'base64',

contents: sa,

};



var attributes = JSON.stringify({

"name": file.name,

"parent": {

"id": "0"

},

});



var bodyList =

'--' + boundary+'"\r\n',

'Content-Disposition:form-data; name="attributes"',

attributes,

'--' + boundary,

'Content-Type:' + file.mimeType,

'Content-Disposition:form-data; name="file"; filename="'+file.name+'"\r\n',

file.contents,

'--' + boundary + '--'

];



request = new sn_ws.RESTMessageV2("Box", "Upload Attachment");

request.setRequestHeader("Accept", "Application/json");

request.setRequestHeader("Content-Type", "multipart/related; boundary=" + boundary);

request.setRequestBody(bodyList);

var response = request.execute();

} catch (err) {

gs.log("error in the try catch: " + err);

}



}


Hi @pratyush_mp ,



I do not see anything wrong with the request it self.



The error message is complaining about not finding the file part.



Does the sa variable actually contains something?



Cheers


Hi @rbarbosa ,



Yes, the sa variable contains the byte version of the file in ServiceNow. I tried to print the value of it which returned “B@7e8de4”.



One more thing which I wanted to know is, while uploading the file to Box, in which format does Box accept the attachment? (Eg: Binary format, Base64 etc.)


@pratyush_mp ,



If I manually upload something using cUrl, like this:



curl --location 'https://upload.box.com/api/2.0/files/content' \

--header 'Content-Type: multipart/form-data' \

--header 'Authorization: Bearer WL9...KNB4v' \

--form 'attributes="{

"name": "Photo.png",

"parent": {

"id": "0"

}

}"' \

--form 'file=@"/Users/rbarbosa/Downloads/box_admin_pic.png"'



It works fine, but I’m not sure what cUrl is doing under the hood. Also all SDK’s accept a binary file stream.



I have no idea how service now works, or what that sn_ws.RESTMessagev2 is doing.



Try both base64 and binary, let’s see what happens.



Cheers


If it helps, here is a small Python function I wrote years ago to use the BoxSDK to upload to a folder. Folder ID is required, as is a live connection to Box. I played around with the requests library in Python for a while before coming back to the BoxSDK:



def upload2Box(boxConn, boxTargetFolderId, sourceFilePath, sourceFilename):

'''upload a file to a folder in Box'''

try:

boxConn.folder(boxTargetFolderId).upload(sourceFilePath, sourceFilename)

return True

except:

return False


Hi @rbarbosa ,



Okay thanks for the information!



Could you provide me the HTTP equivalent of this cURL request? Because here I can see only the Filename which is being passed in the file attribute. Doesn’t give much insight as to how the file “metadata/payload” is being encoded.



Also a quick info about this function “sn_ws.RESTMessageV2()” it is basically a function which calls the REST message which is already configured in ServiceNow. It is used to configure the URL, endpoints, payload etc. for the request which we are making.



In addition to this, I have tried giving both binary and base64 encoded file contents, but received the same error for both. Do I need to mention the encoding type in which the file is encoded in the request which I am sending?


Hi @reynlds-illinois ,



Yup even I tried with the Box SDK in python which makes the whole process very simple.



The problem being that in ServiceNow we don’t have SDKs which we can use to make this possible. So need to configure everything manually. And SDKs just encapsulate everything about the payload which is being sent.


Hi @pratyush_mp



Here it is:



Executing this command:



curl  --trace-ascii /dev/stdout --location 'https://upload.box.com/api/2.0/files/content' \

--header 'Content-Type: multipart/form-data' \

--header 'Authorization: Bearer y788...hkH' \

--form 'attributes="{

"name": "Photo.png",

"parent": {

"id": "0"

}

}"' \

--form 'file=@"/Users/rbarbosa/Downloads/box_admin_pic.png"'



Produces this HTTP request:





0000: POST /api/2.0/files/content HTTP/2

0024: Host: upload.box.com

003a: user-agent: curl/7.88.1

0053: accept: */*

0060: authorization: Bearer y788...uhkH

0098: content-length: 3111

00ae: content-type: multipart/form-data; boundary=--------------------

00ee: ----acf11690303b651d

0104:

=> Send data, 3111 bytes (0xc27)

0000: --------------------------acf11690303b651d

002c: Content-Disposition: form-data; name="attributes"

005f:

0061: {. "name": "Photo.png",. "parent": {. "id": "0". }.}

009d: --------------------------acf11690303b651d

00c9: Content-Disposition: form-data; name="file"; filename="box_admin

0109: _pic.png"

0114: Content-Type: image/png

012d:

012f: .PNG

0135: ......IHDR...@...@.....%.......pHYs................zIDATx..Y{lV.

0175: ..=.{......J..JK[67E....)........,f..........{.]]HD.2.8...d.ef..

01b5: ,....m)..~...n.}..q.......Ic.'..4.w......%U..Yx....L..h.$0.2I`.e



#####################



0bb5: b.$.hwt........@....O..h.$0.2I`.e..D.$...O=...H*..Vy.Y....IEND.B

0bf5: `.

0bf9: --------------------------acf11690303b651d--



Which confirms the file is sent in binary.


I think in your example you’re missing the content length, but please do check each one of the headers and body attributes.



Hope this helps



Cheers


Reply