Third Light Developer Exchange

Code and templating community forum for developers and software integrators

You are not logged in.

Announcement

If you wish to join the Developer Exchange, please contact your account manager - this is to avoid unnecessary spam in the forums. Many thanks for your understanding.

#1 2015-02-27 00:50:57

mdyason
Member
Registered: 2015-01-20
Posts: 43

POSTing an image to the server

Very difficult trying to figure out exactly what to post.  Trying to separate a POST from the syntax for php when I have never looked at PHP before is not working.  The example in C# is easier to understand but still not getting anywhere.

The form data consists of a number of key value pairs - what are all the options that can be used there?

This is what I have so far. 
login using Core.LoginWithKey - and record the sessionID it gives me

we have a user called imsadmin set up as an administrator. 
do a http post using core.ImpersonateUser and passing in imsadmin and record the sessionID from that.

do a http post using folders.CreateFolder

Above post uses sessionID returned by ImpersonateUser and it returns a posturl  "https://ims.mydomain.com/dofileupload.t … tViLtY4Abf"
I added &t=ajax to the end of the URL so that it does not return a html page

I created a post as follows:
URL is "https://ims.mydomain.com/dofileupload.t … Abf&t=ajax"
Added a header "IMSSESSID" with value the sessionID returned from ImpersonateUser
key: "file1"                  value: "c:\x\y\z.jpg"  (where x and y are folder and subfolder on my c drive and z is a filename)
key: "file1_caption"    value: "Nice Sunset"

the message returned from this post was "Sorry, but you do not have access to this page."

What am I doing wrong?

Last edited by mdyason (2015-02-27 00:53:39)

Offline

#2 2015-03-02 17:30:56

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

I tried logging in with core.login with my own username and password.  I definitely have access to the folder.  I get the same result.  I assume I have a syntax issue with the post itself

Offline

#3 2015-03-02 18:28:09

dominic
Third Light Staff
Registered: 2013-06-06
Posts: 96

Re: POSTing an image to the server

Hi,

The immediate problem is that IMSSESSID should be provided as an HTTP cookie - it is not accepted as a header in its own right. You could also avoid this entirely by using the 'allowotheruserupload' option in the call to Uploads.CreateUpload.

The secondary issue that I can see is that you need to send file data in the HTTP post (in multipart/form-data format) - a local path on your machine cannot be opened from IMS.

Kind regards,

Dominic

Offline

#4 2015-03-02 22:37:22

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

Thanks for looking at this dominic.  I was sending the HTTPrequest in multipart/form-data format.

So leaving out the sessionID my HTTP request looks like this:

POST /dofileupload.tlxr?task=tptU5ePPEtViLtY4Abf&t=ajax HTTP/1.1
Host: imstest.matrix-solutions.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file1"

MySunset.jpg
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file1_caption"

Sunset over bay
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="whatGoesHere"; filename="MySunset.jpg"
Content-Type: image/jpeg


 -xxxxxxxxxxxx-
----WebKitFormBoundary7MA4YWxkTrZu0gW

The data for the jpeg is just shown as "-xxxxxxxxxxxx-".  I assume this should be pure binary stream?  (I am using postman to generate this and the text it sends here is the same as you would see if you opened the jpeg using notepad).  What should I name this last piece of form data?  I guess I need to replace the string "whatGoesHere" with something the form handler will understand?  Or should the actual data somehow be sent as part of the formfield that takes the filename?

I have tried doing this in a number of ways, putting quotes in, leaving quotes out etc. No matter what I do, i get exactly the same response:  {"error":true,"message":"Sorry, but you do not have access to this page."}

I am not sure if this is because the syntax of my request is wrong or if the page just will not have anything to do with me because of an access issue.

Last edited by mdyason (2015-03-02 22:46:50)

Offline

#5 2015-03-03 17:28:52

dominic
Third Light Staff
Registered: 2013-06-06
Posts: 96

Re: POSTing an image to the server

The client reference (implicitly file1 in your example) should be used as the name of the part containing the file data - the filename will be parsed out of this content-disposition header. You do not need the file1=MySunset.jpg entry.

As I mentioned previously, to avoid the access denied message you need to either send the session ID as an cookie called IMSSESSID, or specify 'allowotheruserupload' when creating the upload job.

Dominic

Offline

#6 2015-03-03 18:05:16

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

That actually made no difference.  I actually tried exactly this yesterday.  I did it again just now.

My whole process looks as below:  (I ran this through fiddler and made sure that the request going to the server was exactly as intended.  Including the data for the jpeg)

Logon:

POST /api.json.tlx HTTP/1.1
Host: imstest.matrix-solutions.com
Content-Type: application/json

{"apiVersion": "1.0.1", "action": "core.login", "inParams": {"username": "m...", "password": "..."}}

The result was: 
{"result":{"api":"OK","action":"OK"},"outParams":{"sessionId":"qadblRbPRHnBJPv9D4kKQOiN65WWAYYu","userDetails":{"type":1,"userref":"1-4","username":"m...","description":"Michelle .....","email":"....","canPublish":true}}}

CreateUpload:

POST /api.json.tlx HTTP/1.1
Host: imstest.matrix-solutions.com
Content-Type: application/json

{"apiVersion": "1.0.1", "action": "Upload.CreateUpload", 
"inParams": { "params": {"destination": "28168108553", 
                        "synchronous": "true", 
                        "lifetime": "60", 
                        "editablemetadata": { "caption": "OPTIONAL" }, 
                        "allowotheruserupload": "true" }}, 
"sessionId": "qadblRbPRHnBJPv9D4kKQOiN65WWAYYu" }

The result was: 
{"result":{"api":"OK","action":"OK"},"outParams":{"uploadKey":"jgjtzn44ZjASNj-nbaB","postURL":"https:\/\/imstest.matrix-solutions.com\/dofileupload.tlxr?task=jgjtzn44ZjASNj-nbaB"}}

Upload:

POST /dofileupload.tlxr?task=jgjtzn44ZjASNj-nbaB&t=ajax HTTP/1.1
Host: imstest.matrix-solutions.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file1"; filename="MySunset.jpg"
Content-Type: image/jpeg

-xxxxxxxxxxxx-
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file1_caption"

Sunset over bay
----WebKitFormBoundary7MA4YWxkTrZu0gW

result was:
{"error":true,"message":"Sorry, but you do not have access to this page."}

by the way a check using upload.CanUploadToFolder did return true

Last edited by mdyason (2015-03-03 19:30:06)

Offline

#7 2015-03-03 18:17:49

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

Now I'm really confused - after getting the above and posting it here, I looked in the Application and the image is in there, with the caption as shown above.  Looking at upload date and time confirms the above code did in fact upload the image.  But I received the error????

So I tried it again and everything looked exactly the same - the 2nd image never showed up in the app. ????

Last edited by mdyason (2015-03-03 18:42:34)

Offline

#8 2015-03-03 21:37:11

dominic
Third Light Staff
Registered: 2013-06-06
Posts: 96

Re: POSTing an image to the server

It is hard to say for sure without seeing timings, but I do note that the lifetime on the upload task is 60 seconds - so if you attempt to send files after this elapses then you would get the same error. At a guess, the post was sent twice, the first was successful but the second was after the time limit, and it was this second response that you saw.

Offline

#9 2015-03-03 21:46:45

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

Wonderful.  You are exactly correct.  Thank you.  This is now working properly.  That was a pretty stupid mistake on my part.

Offline

#10 2015-03-05 22:17:46

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

I spoke too soon.  The above is working perfectly if I just want to upload a single image, however if I then try and upload a second image using exactly the same code it fails with {"error":true,"message":"Sorry, but you do not have access to this page."}.  Each try I just changed name="file1" to name="file2" and  name="file1_caption" to name="file2_caption" and of course choose a new image with a different name.

Is this expected or not?  How would you go about posting many images (one after the other)?  Surely I can do many posts to the same URL?  Is there something I have to do between uploads?  Surely I do not have to create a new upload ID for each image?  (Which does actually work!)

Last edited by mdyason (2015-03-05 23:25:23)

Offline

#11 2015-03-06 16:29:00

dominic
Third Light Staff
Registered: 2013-06-06
Posts: 96

Re: POSTing an image to the server

I suspect that this is an extension of the issue you were having with the 'lifetime' limit - try setting it to an hour (i.e. 3600), and upload a series of files. That should continue to work until the lifetime expires, or you make an API request to complete/abort the task.

Offline

#12 2015-03-06 17:30:37

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

This was using a lifetime of 3600.  (After realizing my mistake the other day I changed all stored tests to 3600). 
Tried it again this morning - does not work.  Have been doing load testing by creating an upload then uploading one image then creating another upload etc.

The second post looks like this:

POST /dofileupload.tlxr?t=ajax&task=2yB_2yFkxxe2EQm2GOFjf HTTP/1.1
Host: imstest.matrix-solutions.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file2"; filename="1.jpg"
Content-Type: image/jpeg

{photo data here}
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file2_caption"

upload test2
----WebKitFormBoundary7MA4YWxkTrZu0gW

{"error":true,"message":"Sorry, but you do not have access to this page."}

Last edited by mdyason (2015-03-10 18:22:40)

Offline

#13 2015-03-10 18:22:46

mdyason
Member
Registered: 2015-01-20
Posts: 43

Re: POSTing an image to the server

Is this expected or not?  How would you go about posting many images (one after the other)?  Surely I can do many posts to the same URL?  Is there something I have to do between uploads?  Surely I do not have to create a new upload ID for each image?  (Which does actually work!)

Offline

Board footer