Real Software Forums
http://forums.realsoftware.com/

Uploading files with HTTPSocket.Post
http://forums.realsoftware.com/viewtopic.php?f=2&t=46612
Page 1 of 1

Author:  charonn0 [ Fri Jan 18, 2013 6:30 pm ]
Post subject:  Uploading files with HTTPSocket.Post

I've been banging my head against this for some time, yet nothing I do seems to work.

I'm trying to use an HTTPSocket to upload a file to VirusTotal.com using their public API. The relevant part of their (skanty) documentation is here: https://www.virustotal.com/documentatio ... ning-files

My code:
Private Sub SubmitFile(File As FolderItem, APIkey As String)
Const MIMEBoundary = "--------0xKhTmLMIMEBoundary"
Dim CRLF As String = Encodings.ASCII.Chr(13) + Encodings.ASCII.Chr(10)
Dim sock As New HTTPSocket

dim stream as BinaryStream
dim postContent as String
postContent = postContent + "--" + MIMEBoundary + crlf
postContent = postContent + "Content-Disposition: form-data; name=""file"""
postContent = postContent + "; filename=""" + file.AbsolutePath + """"
postContent = postContent + crlf + "Content-Type: application/binary" + crlf + crlf
stream = stream.Open(File)
postContent = postContent + stream.Read( file.length )
stream.close
postContent = postContent + crlf
postContent = postContent + "--" + MIMEBoundary + crlf
postContent = postContent + "Content-Disposition: form-data; name=""apikey"""
postContent = postContent + crlf + "Content-Type: text/plain" + crlf + crlf
postContent = postContent + APIkey
postContent = postContent + crlf
postContent = postContent + "--" + MIMEBoundary + "--"

sock.SetPostContent(postContent, "multipart/form-data, boundary=" + MIMEBoundary)
Dim s As String = Sock.Post("www.virustotal.com/vtapi/v2/file/scan", 5)
Break
End Sub


I get an HTTP 403 error. Any suggestions?

Author:  DaveS [ Fri Jan 18, 2013 6:51 pm ]
Post subject:  Re: Uploading files with HTTPSocket.Post

Quote:
A web server may return a 403 Forbidden HTTP status code in response to a request from a client for a web page or resource to indicate that the server refuses to allow the requested action. In other words, the server can be reached, but the server declined to allow the requested access.



So I'd say your creditials are incorrect

Author:  charonn0 [ Fri Jan 18, 2013 7:02 pm ]
Post subject:  Re: Uploading files with HTTPSocket.Post

The API key is valid; all other API calls work, but no other API calls involve uploading a file.

Author:  J.Sh3ppard [ Fri Jan 18, 2013 8:32 pm ]
Post subject:  Re: Uploading files with HTTPSocket.Post

Hi charonn0

I think Dave answered pretty well but I'll shed more light on it.

I know 403 errors when the server's file permissions are set to NOT allow public access to the file(s) in question.


Assuming your code and their API directions are correct (I didn't check) :

This is not something you as the client can correct. It requires server access to change permission settings which allow you the client access.


If your API code is not correct, then correct it :)

You may want to get an HTTPsocket to upload a file to your own server using php first to make sure your boundaries are correct, etc.


There might be a thread on this in the networking section.

I would offer more help but I'm totally swamped; Not only am I behind my own production schedule but I got suckered into making a website / online business for a family member for free :(

Ps. I really want to WE to be working reliably !
Doing this stuff in php, CSS, HTML, etc. is boring and takes me a long time!!!!

Gotta look more at WE soon and put it to the test.

Author:  Bob Coleman [ Sat Jan 19, 2013 6:08 pm ]
Post subject:  Re: Uploading files with HTTPSocket.Post

I notice that you are passing the absolute path in the filename parameter. This is generally not allowed. Try file.Name instead.

Author:  charonn0 [ Sat Jan 19, 2013 6:13 pm ]
Post subject:  Re: Uploading files with HTTPSocket.Post

Good point, but by itself doesn't seem to help.

Author:  Nux [ Mon Jan 21, 2013 12:47 am ]
Post subject:  Re: Uploading files with HTTPSocket.Post

Hey charonn0,

I spotted this in the link that was posted,

Quote:
If you try to perform calls to functions for which you do not have the required privileges an HTTP Error 403 Forbidden is raised.


is there a way to check your privilege level?

Author:  charonn0 [ Mon Jan 21, 2013 1:19 am ]
Post subject:  Re: Uploading files with HTTPSocket.Post

My privilege level is (AFAIK) the same as everyone else using the public API, which supports uploads.

I have the feeling that I'm getting the 403 error because the API key is not being encoded in the Post request properly. This is the only request I need to hand-roll, so my instinct is that I'm just doing it wrongly.

Author:  swort [ Mon Jan 21, 2013 4:50 am ]
Post subject:  Re: Uploading files with HTTPSocket.Post

You forget 1 thing.

That is to use HTTPSecureSocket instead of HTTPSocket

the docs say:
Quote:
In order to send a file you must perform an HTTP POST request to the following URL:

https://www.virustotal.com/vtapi/v2/file/scan

Author:  charonn0 [ Mon Jan 21, 2013 5:30 am ]
Post subject:  Re: Uploading files with HTTPSocket.Post

It also specifies "API URLs are HTTPS (although you can use traditional HTTP if you wish)"

I intend to use HTTPS once I've sorted out the upload problem; I'm using plain HTTP right now so I can capture the packets being sent to see what the server is actually getting.

Author:  p0wn3d [ Mon Jan 21, 2013 5:39 am ]
Post subject:  Re: Uploading files with HTTPSocket.Post

Sock.Post("www.virustotal.com/vtapi/v2/file/scan", 5)


What does the #5 in the sock string mean just out of curiosity?

Author:  swort [ Mon Jan 21, 2013 2:49 pm ]
Post subject:  Re: Uploading files with HTTPSocket.Post

charonn0 wrote:
It also specifies "API URLs are HTTPS (although you can use traditional HTTP if you wish)"

I intend to use HTTPS once I've sorted out the upload problem; I'm using plain HTTP right now so I can capture the packets being sent to see what the server is actually getting.


Perhaps they mean non-authenticated api parts.
I won't think they let you do any login on http based api parts.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/