Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using sf::Http to send file  (Read 3414 times)

0 Members and 1 Guest are viewing this topic.

tofiffe

  • Newbie
  • *
  • Posts: 8
    • View Profile
Using sf::Http to send file
« on: May 05, 2013, 02:02:29 pm »
I have a webpage, that accepts a file and name for it on server.
for the post so far I have:
sf::Http http;
http.setHost("http://localhost/shost");
sf::Http::Request request;
request.setMethod(sf::Http::Request::Post);
request.setBody("file=C:\\path\\to\\file.txt&name=something");
sf::Http::Response response=http.sendRequest(request);
if(response.getStatus()==sf::Http::Response::Ok)
{
        cout<<response.getBody();
}
else
{
        cout<<"error "<<response.getStatus()<<endl;
}

I get error 1001 which appears to be a file transfer error, how can I fix this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using sf::Http to send file
« Reply #1 on: May 05, 2013, 02:28:21 pm »
1001 is ConnectionFailed.

Try to set the URI explicitly, even if it's index.html. I don't know if "shost" is part of the server name, but if it's not (i.e. it's rather a sub-folder of the server), it should be part of the URI instead.
Laurent Gomila - SFML developer

tofiffe

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Using sf::Http to send file
« Reply #2 on: May 05, 2013, 02:49:38 pm »
okay, moving the shost to request worked, now I'm not getting any errors, however the file isn't uploaded...am I supposed to do anything else with it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using sf::Http to send file
« Reply #3 on: May 05, 2013, 03:08:34 pm »
You're just sending a string with the (local!) path of your file inside, did you really expect this to magically upload the file's content to your server? :P

If you want to send the file, you must put its contents into your request's body, and set the MIME type (it's a header field) to the corresponding type. Your server must then process it to do whatever you want with it (like storing it on the server's space).
Laurent Gomila - SFML developer

tofiffe

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Using sf::Http to send file
« Reply #4 on: May 05, 2013, 03:16:53 pm »
Can you perhaps give me an example how this can be done?

Let me see if I got this correct:
1. I have to read the file in binary and replace the path with it
2. Set the header to the specified file type (which I'm not quite sure how)

Anything else I should do on the server?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using sf::Http to send file
« Reply #5 on: May 05, 2013, 04:51:22 pm »
Quote
1. I have to read the file in binary and replace the path with it
Yes.

Quote
2. Set the header to the specified file type (which I'm not quite sure how)
request.setField("mime-type", "image/png");

(this is just an example, header name and/or MIME type may not be correct, you should look for the exact names)

And more generally, you should really learn the basics of HTTP before trying to use it. You can't learn the full thing on this forum.

Quote
Anything else I should do on the server?
Read some PHP / image upload tutorial maybe? :P
Laurent Gomila - SFML developer

tofiffe

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Using sf::Http to send file
« Reply #6 on: May 05, 2013, 06:26:11 pm »
I've got the server set up, the upload works in browsers, now, I've tried sending the file to server but since in the file there are null characters that I cannot append to string - only up to that point, the rest gets cut off...

from what I read it is supposed to be
request.setField("Content-Type", "multipart/form-data");

this however gives this error: Missing boundary in multipart/form-data POST data on line 0
also the content length on page equals to ~20 characters, from which 16 are the "file=" and "&fname=blah", so there are only 4 characters left for the file.

Needles to say, file is not uploaded.

edit: after adding the ";boundary=(size of file+16)" the content length is now matching, still the file is not uploaded.
« Last Edit: May 05, 2013, 06:40:10 pm by tofiffe »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Using sf::Http to send file
« Reply #7 on: May 18, 2013, 12:24:56 pm »
in the file there are null characters that I cannot append to string
Why not? std::string has no problems with '\0'. Its length is stored in a separate variable and not determined by the null terminator.

How are you reading your file? The right approach would be to create an empty string of the file's size, then read the whole binary file into the string. Maybe you should have a detailed look at std::ifstream.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: