• API problems with C++
    14 replies, posted
Ok so i discovered the API today, and i started playing around with it, but i seem to be getting some mixed results before i say anything im using the HTTP class from SFML2.0, this is also my first time doing http requests and such in C++ My Code [CODE] #include <iostream> #include <string> #include <SFML\Network.hpp> #include <stdio.h> #include "md5.h" using namespace std; void testUser(string name, string pass, sf::Http &http) { string call = "http://api.facepun.ch/?username=&password=&action=authenticate"; call.insert(32,name); call.insert((42+name.length()),pass); cout << call << endl; sf::Http::Request request(call); sf::Http::Response responce = http.sendRequest(request); cout << responce.getBody() << endl; } int main() { sf::Http http; http.setHost("http://api.facepun.ch"); string userName; string password; cin >> userName; cin >> password; MD5 md5; char *tmp_pass = new char[password.size() + 1]; copy(password.begin(),password.end(),tmp_pass); tmp_pass[password.size()] = '\0'; cout << tmp_pass << endl; password = md5.digestString(tmp_pass); cout << password << endl; string md5Pass = password; testUser(userName,md5Pass,http); return EXIT_SUCCESS; } [/CODE] My output (the Password and md5 are censored clearly) [url]http://imgur.com/5Hfi6[/url] So my question is, why am i getting the 404? like i said im new to this whole http requests, and what not, so any suggestions? i feel like im doing something wrong, because if its making the connections, shouldn't it be saying "error" in the body not 404? or should i be using some other function?
[del]try making an http request to [url]www.google.com[/url] to see if its your code or the site[/del] nevermind, if the request failed you wouldn't be receiving an html 404 page in the first place
[QUOTE=Kopimi;35911602][del]try making an http request to [url]www.google.com[/url] to see if its your code or the site[/del] nevermind, if the request failed you wouldn't be receiving an html 404 page in the first place[/QUOTE] Yea i was just about to post somthing, i added a line of code to check the status and it says im getting a 400 error which is a "Bad Request" in sfml, anyone know if i cant use SFML, or do i need a new library? [editline]11th May 2012[/editline] Could it be that because the responses are JSON that SFML sees it as an error? i mean it doesn't return a HTTP page.
[QUOTE=MickeyCantor;35911661]Yea i was just about to post somthing, i added a line of code to check the status and it says im getting a 400 error which is a "Bad Request" in sfml, anyone know if i cant use SFML, or do i need a new library? [editline]11th May 2012[/editline] Could it be that because the responses are JSON that SFML sees it as an error? i mean it doesn't return a HTTP page.[/QUOTE] I don't think its because json response. Maybe you better go with C# if you don't know what you are doing :P
[QUOTE=burak575;35911700]I don't think its because json response. Maybe you better go with C# if you don't know what you are doing :P[/QUOTE] i mean id rather not go to C#, i have been doing alot of learning of C++ and i would like to actualy get started working on actual projects, not just small examples and what not. I mean why change to a different language just because i haven't done http requests, when i can just learn them in C++
Just checked the api and you probably doing it wrong. In request you shouldn't include the hostname again. [CODE] string call = "http://api.facepun.ch/?username=&password=&action=authenticate"; call.insert(32,name); call.insert((42+name.length()),pass);[/CODE] make it something like this instead random inserts. [CODE] char buff[256]; sprintf(buff, "?username%s=&password%s=&action=authenticate", name.c_str(),pass.c_str()); string call = buff; [/CODE]
[QUOTE=burak575;35911726]Just checked the api and you probably doing it wrong. In request you shouldn't include the hostname again. [CODE] string call = "http://api.facepun.ch/?username=&password=&action=authenticate"; call.insert(32,name); call.insert((42+name.length()),pass);[/CODE] make it something like this instead random inserts. [CODE] char buff[256]; sprintf(buff, "?username%s=&password%s=&action=authenticate", name.c_str(),pass.c_str()); string call = buff; [/CODE][/QUOTE] idk i tried just using the call without restating the hostname, and still an error and the API page says to address the calls as "https://api.facepun.ch/?username=<username>&password=<password MD5 hash>&action=getforums" so idk
[QUOTE=MickeyCantor;35911718]i mean id rather not go to C#, i have been doing alot of learning of C++ and i would like to actualy get started working on actual projects, not just small examples and what not. I mean why change to a different language just because i haven't done http requests, when i can just learn them in C++[/QUOTE] Well because sfml is not made for making HTTP requests but you can find everything in .net framework which is proven to working. C++ is like master of all stuff. Like nearly every stuff is made by it. So you can find a lot of different libraries to accomplish something in C++. This means a lot of APIs you may need to learn. Once you know enough this will not be a problem and you can live happily ever after :) Until that point C# will be less frustrating. It was just a suggestion anyway. [editline]11th May 2012[/editline] [QUOTE=MickeyCantor;35911772]idk i tried just using the call without restating the hostname, and still an error and the API page says to address the calls as "https://api.facepun.ch/?username=<username>&password=<password MD5 hash>&action=getforums" so idk[/QUOTE] If you look closer its "https" not "http" maybe something about this...
[QUOTE=burak575;35911775]Well because sfml is not made for making HTTP requests but you can find everything in .net framework which is proven to working. C++ is like master of all stuff. Like nearly every stuff is made by it. So you can find a lot of different libraries to accomplish something in C++. This means a lot of APIs you may need to learn. Once you know enough this will not be a problem and you can live happily ever after :) Until that point C# will be less frustrating. It was just a suggestion anyway.[/QUOTE] fair enough and i get were you coming from. if SFML is a poor choice for http requests, do you know of any good library's that can be used for http requests? it also says requests can be made over http though, i tried using https (SMFL supports it) and still the 400 error
[QUOTE=MickeyCantor;35911789]fair enough and i get were you coming from. if SFML is a poor choice for http requests, do you know of any good library's that can be used for http requests? it also says requests can be made over http though, i tried using https (SMFL supports it) and still the 400 error[/QUOTE] A quick search on google and I found about POCO lib. Didn't used it but I remember some time I was downloaded it. Looks like more "complete" library than going with sfml :)
I should probaly sleep, ill look into it after this weekend, thanks for your help though
It has md5 stuff too. No problem, have a nice sleep then :)
[QUOTE=burak575;35911726]Just checked the api and you probably doing it wrong. In request you shouldn't include the hostname again. [CODE] string call = "http://api.facepun.ch/?username=&password=&action=authenticate"; call.insert(32,name); call.insert((42+name.length()),pass);[/CODE] make it something like this instead random inserts. [CODE] char buff[256]; sprintf(buff, "?username%s=&password%s=&action=authenticate", name.c_str(),pass.c_str()); string call = buff; [/CODE][/QUOTE] he prints the request that he's sending into console and it's formatted fine a lot of people recommend [url]http://curl.haxx.se/[/url] for http requests in c++ try removing the "http://api.facepun.ch/" from your "call" variable, seeing as you're already setting it as the host of the request i'm guessing that you're basically sending a request that includes the hostname, i.e. "http://api.facepun.ch/http://api.facepun.ch/?username=_&password=_&action=authenticate" [editline]11th May 2012[/editline] [QUOTE=burak575;35911775]Well because sfml is not made for making HTTP requests but you can find everything in .net framework which is proven to working. C++ is like master of all stuff. Like nearly every stuff is made by it. So you can find a lot of different libraries to accomplish something in C++. This means a lot of APIs you may need to learn. Once you know enough this will not be a problem and you can live happily ever after :) Until that point C# will be less frustrating. It was just a suggestion anyway. [editline]11th May 2012[/editline] If you look closer its "https" not "http" maybe something about this...[/QUOTE] switching to an entirely new language just because he's messing up http requests in a library that is primarily for 2d rendering assistance is completely unnecessary also using https doesn't make a difference with facepunch api [editline]11th May 2012[/editline] actually yeah i think thats what it is because visiting this link: [url]http://api.facepun.ch/http://api.facepun.ch/?username=_&password=_&action=authenticate[/url] gives you the 404 page that is being returned, so remove "http://api.facepun.ch/" from your "call" string and everything should work fine
[QUOTE=Kopimi;35911851]he prints the request that he's sending into console and it's formatted fine a lot of people recommend [url]http://curl.haxx.se/[/url] for http requests in c++ try removing the "http://api.facepun.ch/" from your "call" variable, seeing as you're already setting it as the host of the request i'm guessing that you're basically sending a request that includes the hostname, i.e. "http://api.facepun.ch/http://api.facepun.ch/?username=_&password=_&action=authenticate" [editline]11th May 2012[/editline] switching to an entirely new language just because he's messing up http requests in a library that is primarily for 2d rendering assistance is completely unnecessary also using https doesn't make a difference with facepunch api [editline]11th May 2012[/editline] actually yeah i think thats what it is because visiting this link: [url]http://api.facepun.ch/http://api.facepun.ch/?username=_&password=_&action=authenticate[/url] gives you the 404 page that is being returned, so remove "http://api.facepun.ch/" from your "call" string and everything should work fine[/QUOTE] Cmon its not entirely new language. Think its like automatic c++ with a nice library called .net framework. Good for beginners and you can always switch between. Not a big deal. I knew he prints the request but I already said he should remove the part you mentioned already. I just make it look nicer instead of counting the characters and doing an insert to that point which is obviously not a good programming practice.
Ok so both of you were right, i had to remove the source from my call, and now what im getting is my program telling me "terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::insert, then it crashes and tells me "This application has requested the runtime to terminate it in an unusual way......ect" so idk why it crashes but i think it has something to do with mu strings and char's? idk im not a master of the stl Im dumb, i forgot to change my pram's of the insert function................................... Im a noob [editline]11th May 2012[/editline] [url]http://imgur.com/nyDRF[/url] [url]http://www.youtube.com/watch?v=AbL3A_GT9uw[/url]
Sorry, you need to Log In to post a reply to this thread.