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

Author Topic: downloading image for sfml resource use  (Read 3644 times)

0 Members and 1 Guest are viewing this topic.

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
downloading image for sfml resource use
« on: November 12, 2013, 05:19:17 am »
I havent dabbed at all yet into network of sfml yet. My overall goal was to make a function in which could download an image for me so that i could use it as a sprite. I skimmed the network docs but i dont see such related exactly to it. I pieced together code from what i found on these forums. However the object http was not mentioned in the thread as to what is was. So i guessed as if it was sf::Http. However when doing that though i get errors. I am not sure what exactly is wrong? What am i missing?

#include <SFML/Network.hpp>
#include <fstream>

int main(){
    std::string url = "http://s1297.photobucket.com/user/metulburr/media/walker_zpsbd2d34be.png.html";
    sf::Http http;
    sf::Http::Request req(url);
    sf::Http::Response image_res = http.sendRequest(req);

    std::string body = image_res.getBody();
    std::ofstream file;
    file.open("tux.png", std::ios::binary);
    file.write(body.c_str(), body.size());
    file.close();
}

the thread i am talking about is:
http://en.sfml-dev.org/forums/index.php?topic=8160.0
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: downloading image for sfml resource use
« Reply #1 on: November 12, 2013, 07:45:06 am »
Quote
However when doing that though i get errors.
Are we supposed to guess which errors? ;)
Laurent Gomila - SFML developer

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: downloading image for sfml resource use
« Reply #2 on: November 12, 2013, 07:47:50 am »
Your code compiles and runs without any error, and since you didn't say which errors you had...  ::)
You can also check the status code of the response to know more about what went wrong. And I'd suggest you use Http and Request like they are used in the documentation. ;)

BTW trying to save an .html page inside a .png isn't always a great idea.
« Last Edit: November 12, 2013, 08:05:37 am by G. »

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: downloading image for sfml resource use
« Reply #3 on: November 12, 2013, 01:04:23 pm »
oh whoops sorry.

The error i get is:
Code: [Select]
/tmp/ccRlWxMK.o: In function `main':
test2.cpp:(.text+0x5f): undefined reference to `sf::Http::Http()'
test2.cpp:(.text+0xae): undefined reference to `sf::Http::Request::Request(std::string const&, sf::Http::Request::Method, std::string const&)'
test2.cpp:(.text+0xf0): undefined reference to `sf::Http::sendRequest(sf::Http::Request const&, sf::Time)'
test2.cpp:(.text+0xff): undefined reference to `sf::Http::Response::getBody() const'
/tmp/ccRlWxMK.o: In function `sf::TcpSocket::~TcpSocket()':
test2.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x2f): undefined reference to `sf::Socket::~Socket()'
/tmp/ccRlWxMK.o:(.rodata._ZTIN2sf9TcpSocketE[_ZTIN2sf9TcpSocketE]+0x10): undefined reference to `typeinfo for sf::Socket'
collect2: error: ld returned 1 exit status
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: downloading image for sfml resource use
« Reply #4 on: November 12, 2013, 01:07:55 pm »
You must link to sfml-network.
Laurent Gomila - SFML developer

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: downloading image for sfml resource use
« Reply #5 on: November 12, 2013, 03:11:48 pm »
omg, that was a stupid mistake

thank you
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: downloading image for sfml resource use
« Reply #6 on: November 12, 2013, 08:29:06 pm »
OK now though the image is not able to open. It doenst save the image as expected? Is there something else missing that is stopping this?
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

kimci86

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Re: downloading image for sfml resource use
« Reply #7 on: November 12, 2013, 09:22:18 pm »
You have to set http host target and only give the URI to the request constructor.
Have a look at the documentation

This code should work. Notice I use a reference to response body so it is not copied in a new string.
#include <SFML/Network.hpp>
#include <fstream>

int main(){
    sf::Http http("i1297.photobucket.com");
    sf::Http::Request req("/albums/ag23/metulburr/walker_zpsbd2d34be.png");
    sf::Http::Response image_res = http.sendRequest(req);

    const std::string& body = image_res.getBody();
    std::ofstream file("tux.png", std::ios::binary);
    file.write(body.c_str(), body.size());
}

 

anything