Rest API Call setBinaryField() for sending attachment to Rol

Posted by IramK on 03-Nov-2014 03:19

Hello,

I am trying to send an attachment (.pptx) to Rollbase via REST API call. I am using the setBinaryField api call to perform this task and the string that I am using is given below:

updateRecordStr = ServerStr & "/rest/api/setBinaryField?sessionId=" & sessionID & "&id=" & recordIDStr & "&fieldName=file&value=" & attachStr & "&fileName=xyz.pptx" "&contentType=application/vnd.openxmlformats-officedocument.presentationml.presentation"

Is the above given string ok? Because when I am performing an HTTP send, my application stops working.

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", FullURLPath, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.Send ("")

I am also getting an error: Unknown API for POST request setBinaryField. Do you guys find any problem with the API?

Posted by Ricardo Rafols on 04-Nov-2014 15:11

Hi Iramk,
 
Take a look.. it is working…
 
Remember to update for your sessionid, field name, id and etc…
 
Regards,
Ricardo
 
 
Code.txt
Progress64.txt
 
 

All Replies

Posted by IramK on 03-Nov-2014 10:30

I have been able to get past the Unknown API for POST request setBinaryField by replacing that function with setDataField. But this time I am getting another error i.e.

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>414 Request-URI Too Large</title>

</head><body>

<h1>Request-URI Too Large</h1>

<p>The requested URL's length exceeds the capacity

limit for this server.<br />

</p>

<hr>

<address>Apache/2.2.27 (Amazon) Server at www.rollbase.com Port 443</address>

</body></html>

May I know why its giving this error?

Posted by pvorobie on 03-Nov-2014 11:22

Try moving URL parameters to objHTTP.Send ("") call.

Posted by IramK on 03-Nov-2014 11:24
Posted by IramK on 03-Nov-2014 11:25

Can you give me an example please? I have added my complete URL path.

Posted by Godfrey Sorita on 03-Nov-2014 14:31

setDataField seems to work on small files. When I encoded a 1kb PNG file to BASE64 and used it on value parameter, the REST request worked. Though when I used an 11kb PNG file, it did not work nor returned any error.

Below is a sample code written in Ruby:

require 'httparty'

class RestGet
  include HTTParty
  base_uri 'http://localhost:8830/rest/api'

  def initialize ()
    
  end

  def login(username, password, output)
    options = { query: {loginName: username, password: password, output: output} }
    self.class.get("/login", options)
  end
  
  def getBinaryData(sessionId, objName, id, fieldName)
    options = { query: {sessionId: sessionId, objName: objName, id: id, fieldName: fieldName} }
    self.class.get("/getBinaryData", options)
  end

  def setDataField(sessionId, objName, id, fieldName, value, contentType, fileName, output)
    options = { query: {sessionId: sessionId, objName: objName, id: id, fieldName: fieldName, value: value, contentType: contentType, fileName: fileName, output: output} }
    self.class.post("/setDataField", options)  
  end

end

RestInstance = RestGet.new()
RestLogin = RestInstance.login("customer1", "yourpassword", "json") #Generate sessionId for REST transaction
sessionId = RestLogin["sessionId"] 
puts "sessionId:" + sessionId 

RestgetBinaryData = RestInstance.getBinaryData(sessionId, "student", 14304, "file") #Get Binary Data of "file" from Record 14304
#puts "\n<<==Binary data start==>>\n" + RestgetBinaryData + "\n<<==Binary data end==>>\n"

b64BinaryData = Base64::encode64(RestgetBinaryData) # Encode binary data to Base64
#puts "\n<<==Base 64 encoded data start==>>\n" + b64BinaryData + "\n<<==Base 64 encoded data end==>>\n"

RestSetDataField = RestInstance.setDataField(sessionId, "student", 14305, "file", b64BinaryData, "image/png", "ua.png", "json") #Set File Upload field "file" of Record 14305
puts RestSetDataField


Can you also test uploading a small file using your script? 

Thanks,

Godfrey

Posted by pvorobie on 03-Nov-2014 15:09

Please make sure that your code handles multipart HTTP requests correctly.

Posted by IramK on 04-Nov-2014 03:10

Hello Godfrey,

Yes I have tried uploading a small text file using my script and it works every time. But I haven't been able to do the same for a .pptx, .docx, .pdf file. I am trying to make sure that one rbfSet (binary/data field) does the job for all files regardless of type by specifying their respective MIME types. Any suggestions on that?

Posted by IramK on 04-Nov-2014 04:09

I have tried this now and it looks like its almost there.

objHTTP.Open "POST", ServerStr & "/rest/api/setDataField?", False

               objHTTP.setRequestHeader "Content-Type", "multipart/form-data; Charset=UTF-8; boundary=" + Boundary

               objHTTP.setRequestHeader "Content-Length", Len(attachStr)

               objHTTP.setRequestHeader "Content-Disposition", "form-data; name='file'"

               objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

               objHTTP.Send ("sessionId=" & IdRet & "&id=" & recordIDStr & "&fieldName=file&value=" & attachStr & _

               "&contentType=application/vnd.openxmlformats-officedocument.presentationml.presentation&fileName=" & attachNameStr)

Getting this error now:

<?xml version="1.0" encoding="UTF-8" ?>

<resp status="fail">

<err>Corrupt form data: premature ending</err>

</resp>

Any suggestions?

Posted by Ricardo Rafols on 04-Nov-2014 07:23

Hi Iram,

I am  testing REST using SOAPUI 5.0.0.

this is my raw file.. maybe it can help you.

POST http://localhost:8080/rest/api/setDataField HTTP/1.1

Accept-Encoding: gzip,deflate

Content-Type: application/x-www-form-urlencoded

Content-Length: <automatic calculated>

Host: <automatic populated>

Connection: Keep-Alive

User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

sessionId=<csession id>&id=<record id>&fieldName=<object field name>&fileName=<file name + extention>&objName=<object name>&contentType=<content type>=<base64 file>

my example:

sessionId=cfa868a212c14066910790c7096bded3%401&id=306212&fieldName=file&fileName=test.pdf&objName=teste_templates&contentType=application%2Fpdf&value=<base 64 file here>

I used a 67k pdf file and it worked.

I converted my file using www.motobit.com/.../base64-decoder-encoder.asp and removed all "enters" between the lines.

Posted by IramK on 04-Nov-2014 08:30

Hello Ricardo,

Could you tell me as to how did you get the Content Length? I am using VBA and I calculated my content length from the string length of my base64 string. Is that correct? Also now it partially seems to work but now I am getting an error : No URL parameter "sessionId" (case-sensitive)

Dim ContentLength As Long

ContentLength = Len(attachStr)

objHTTP.Open "POST", ServerStr & "/rest/api/setDataField?", False

objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

objHTTP.setRequestHeader "Content-Length", ContentLength

objHTTP.setRequestHeader "Content-Disposition", "form-data;name='file';filename='RB_User_Guide.pdf"

objHTTP.setRequestHeader "User-Agent", "Apache-HttpClient/4.1.1"

objHTTP.Send ("sessionId=" & IdRet & "&objName=temphistoryattachment&id=" & recordIDStr & "&fieldName=file&value=" & attachStr & "&contentType=application/pdf&fileName=" & attachNameStr & "&output=xml")

Output:

<?xml version="1.0" encoding="UTF-8" ?>

<resp status="fail">

<err>No URL parameter "sessionId" (case-sensitive)</err>

</resp>

Posted by Ricardo Rafols on 04-Nov-2014 08:44

The content length is the total characters of your objHTTP.Send.

my example:

sessionId=59d93c0189c346fdaa20e54459313d83%401&id=306212&fieldName=file&fileName=test.pdf&objName=teste_templates&contentType=application%2Fpdf&value=teste

the length is 155.

when I change for a base 64, the length is 106006

another example:

sessionId=59d93c0189c346fdaa20e54459313d83%401&id=306212&fieldName=file&fileName=test.pdf&objName=teste_templates&contentType=application%2Fpdf&value=teste&output=xml

the length is 166.

The error No URL parameter "sessionId" (case-sensitive) means your session ID is expired or not filled.

Posted by IramK on 04-Nov-2014 09:18

I'm not sure why but I have used the same sessionId everywhere else and it just works everytime but when I come to this code, it expires? I have checked whether there is a sessionId or not and it always is available. Also I tried getting a new session Id but that displayed the same error. "No URL parameter "sessionId"".


Also, I noticed something peculiar. Files with a small size gets uploaded properly (with correct size but cannot open those files via Rollbase) but the ones with a larger size (about 2MB) get back with that error. 

Posted by Ricardo Rafols on 04-Nov-2014 09:46

I was able to replicate the error.

It happens when I tried to call the API without sending any parameter.

http://localhost:8080/rest/api/setDataField

<resp status="fail">

<err>No URL parameter "sessionId" (case-sensitive)</err>

</resp>

Posted by IramK on 04-Nov-2014 09:56

I am guessing the URL length is going out of hand for it to cope up with the file size which is possibly why the send is not performing properly. Is your base64 string immensely huge as well?

Posted by pvorobie on 04-Nov-2014 10:53

Please remove "?" from URL

Posted by IramK on 04-Nov-2014 11:09

same error even after removing the ? from the URL..

Posted by IramK on 04-Nov-2014 11:17

Tried using SOAP 5.0.0. Getting a URI too large error.

Posted by pvorobie on 04-Nov-2014 11:22

Are you sending long file? If so, it may be necessary to use multipart HTTP calls.

Posted by IramK on 04-Nov-2014 11:23

Yes I am sending a long file. could you please provide an example of the multipart HTTP call?

Posted by pvorobie on 04-Nov-2014 11:25

Actually REST API has name setBinaryData, not setBinaryField. That's why you're having error "Unknown API".

Posted by IramK on 04-Nov-2014 11:28

Yes I know, I tried setBinaryData... same result. (don't get the error anymore) but still get the No URL parameter sessionId.

Posted by pvorobie on 04-Nov-2014 12:05

Are you also getting " Unknown API for POST request" error when using setBinaryData API?

Posted by pvorobie on 04-Nov-2014 12:06

"No URL parameter sessionId" seems like a different problem. I think server cannot read your HTTP request and extract URL parameters.

Posted by Ricardo Rafols on 04-Nov-2014 15:11

Hi Iramk,
 
Take a look.. it is working…
 
Remember to update for your sessionid, field name, id and etc…
 
Regards,
Ricardo
 
 
 
 

Posted by IramK on 05-Nov-2014 03:26

Btw Ricardo, thanks for that URLEncode function, that is the function that I wasn't using in my code that has made it work for smaller files. By the way I would like to mention that I am using a Trial account of Rollbase, I'm not sure if that affects it or not. The problem still persists for larger files i.e. I still get the error: 'No URL parameter "sessionId"' (for larger files). Would the larger file upload be related to me using a Trial account?

The maximum file size I am able to achieve is about 1.3 MB. Anything above that just returns back with the above mentioned error.

Posted by Ricardo Rafols on 05-Nov-2014 06:49

No IramK,

Did you get this error using VBA or SOAPUI?

Using SOAPUI you have to check the Option "Controls if query-parameters should be put in message body" (option next to Media Type.

Posted by Ricardo Rafols on 05-Nov-2014 07:59

Hi IramK,

In the Public Cloud (Rollbase.com),  there is a limit of 2MB for File upload.

Posted by IramK on 05-Nov-2014 10:11

Yes so all makes sense now. The 2MB limit and the URL encoding. It works now.

This thread is closed