yhirose / cpp-httplib

A C++ header-only HTTP/HTTPS server and client library
MIT License
13.11k stars 2.31k forks source link

A considerable amount of errors while including HTTPLib in a project. #1152

Closed ghost closed 2 years ago

ghost commented 2 years ago

streamable video url

As you can see, my project is completely unbuildable.

My code:

#define CPPHTTPLIB_OPENSSL_SUPPORT

#include "guilded.h"

#include "../include/httplib.h"

#include <iostream>
#include <string>

#include <vector>

using namespace std;

/**
 * Login to a Guilded account.
 *
 * @param email The email to log into.
 * @param password The password to log into.
 * @return A Guilded authorization response, with the response token, body, status and text status code.
 */
AuthResponse Guilded::login(string email, string password) {
    httplib::Client cli("https://www.guilded.gg/api");

    auto res = cli.Post("/login", "{ \"email\" : \"" + email + "\", \"password\" : \"" + password + "\" }", "application/json");

    map<string, string> cookies = map<string, string>();

    AuthResponse rsp = {
            rsp.body = res->body,
            rsp.status = res->status,
            rsp.auth = "ggg"
    };

    if (rsp.status == 200)
        rsp.statusName = "OK";
    else if (rsp.status == 201)
        rsp.statusName = "CREATED";
    else if (rsp.status == 204)
        rsp.statusName = "NO CONTENT";
    else if (rsp.status == 304)
        rsp.statusName = "NOT MODIFIED";
    else if (rsp.status == 400)
        rsp.statusName = "BAD REQUEST";
    else if (rsp.status == 401)
        rsp.statusName = "UNAUTHORIZED";
    else if (rsp.status == 403)
        rsp.statusName = "FORBIDDEN";
    else if (rsp.status == 404)
        rsp.statusName = "NOT FOUND";
    else if (rsp.status == 405)
        rsp.statusName = "METHOD NOT ALLOWED";
    else if (rsp.status == 502)
        rsp.statusName = "GATEWAY UNAVAILABLE";
    else if (rsp.status == 429) {
        rsp.statusName = "TOO MANY REQUESTS";
        printf("");
    }

    return rsp;
}

/**
 * Uploads a file to the Guilded network.
 *
 * @param bin Binary data of the file.
 * @param type The file extension, added to the end of the mime type image/ .
 * @return A Guilded response, with the response body, status and text status code.
 */
Response Guilded::upload(vector<char> bin, string name, string type) {
    httplib::Client cli("https://media.guilded.gg/media");

    httplib::Params params; /** i can't initialize this normally **/

    params.emplace("file", string(bin.begin(), bin.end())); /** by the way, emplace wasn't found according to Eclipse. **/

    auto res = cli.Post("/upload", params);

    Response rsp = {
            rsp.body = res->body,
            rsp.status = res->status
    };

    if (rsp.status == 200)
        rsp.statusName = "OK";
    else if (rsp.status == 201)
        rsp.statusName = "CREATED";
    else if (rsp.status == 204)
        rsp.statusName = "NO CONTENT";
    else if (rsp.status == 304)
        rsp.statusName = "NOT MODIFIED";
    else if (rsp.status == 400)
        rsp.statusName = "BAD REQUEST";
    else if (rsp.status == 401)
        rsp.statusName = "UNAUTHORIZED";
    else if (rsp.status == 403)
        rsp.statusName = "FORBIDDEN";
    else if (rsp.status == 404)
        rsp.statusName = "NOT FOUND";
    else if (rsp.status == 405)
        rsp.statusName = "METHOD NOT ALLOWED";
    else if (rsp.status == 502)
        rsp.statusName = "GATEWAY UNAVAILABLE";
    else if (rsp.status == 429)
        rsp.statusName = "TOO MANY REQUESTS";

    return rsp;
}

Using the WinBuilds distribution of the G++ compiler, making a library with AR.

yhirose commented 2 years ago

@acaiberii, you probably forget so set -std=c+11. Also please see the note in README for more information regarding g++ compiler. https://github.com/yhirose/cpp-httplib#note

ghost commented 2 years ago

I'd like you to reopen this.

here

After upgrading to G++ 10.2.0 and specifying -std=c++11 (-std=c+11 doesnt work), I get a LOT of errors.
This also gives warnings and errors about my actual code in guilded.cpp and guilded.h. I'd switch to libcpr if I could but I don't want to use Conan.

PixlRainbow commented 2 years ago

on Windows, you need to link windows sockets (e.g. -lws2_32).

mcordova1967 commented 2 years ago

Hello @acaiberii, I am compiling on Windows 10 x64 with G++ 11.2.0 using these options:

1) Includes (in this order), I included openssl because I want to set some options, otherwise it's not needed, many others below httplib.h are specific to the needs of my program.

define CPPHTTPLIB_OPENSSL_SUPPORT

include

include

include <sys/types.h>

include

include

include

include

include

include

include

include <openssl/ssl.h>

include <openssl/opensslv.h>

2) compiler CMD: g++ -I"C:\eclipse4c\include" -O3 -Wall -c -fmessage-length=0 -std=c++20 -MMD -MP -MF"src/microserver.d" -MT"src/microserver.o" -o "src/microserver.o" "../src/microserver.cpp"

Note: "C:\eclipse4c\include" contains openssl includes directory as well as my own .h files, only those.

3) Linker CMD: g++ -L"C:\eclipse4c\lib" -o "microserver" ./src/microserver.o -lodbc32 -lssl -lcrypto -lws2_32

Note: "C:\eclipse4c\lib" contais openssl libraries: libcrypto.a libcrypto.dll.a libssl.a libssl.dll.a

I use ODBC, otherwise, the -lodbc32 is not required.

It's very important to have the \bin folder of G++ in your system or user path. I use Eclipse CDT, if the path is not set then Eclipse won't work and a lot of errors will be generated, I also have to set the options shown above for compiler and linker.

The same source code compiles on Linux (Ubuntu 20.04) with almost identical commands, except for the linker, I don't have to link with -lws2_32 and the -pthreads is required for the linker on Linux. The path for openssl libs changes too.

Regards, Martin

ghost commented 2 years ago

@mcordova1967 Including OpenSSL from C:\openssl\include in the INCLUDE environmental variable. The include includes the folders crypto, openssl and internal which HTTPLib all uses.

Batch:

@echo off
g++ -I"C:\openssl\include" -O3 -Wall -c -fmessage-length=0 -std=c++20 -MMD -MP -MT"build/objects/guilded.o" -o "build/objects/guilded.o" "src/guilded.cpp"
ar rcs ./build/lib/libguilded.a ./build/objects/guilded.o

Running this batch file produced the same amount of errors, even more than the original command.

G++ in C:\msys64\mingw64\bin linked in system path. No G++ bin\ folder.

mcordova1967 commented 2 years ago

@acaiberii : That's the /bin folder I am referring to. If you are using c++ 10.x you probably don't have support for -std=c++20, but that's not relevant as long as you use the minimum standard supported by httplib.

Maybe the first error messages printed by the compiler are the most important, try using -std=c++14 and also check the order of your includes, is httplib.h the first one? that caused me some trouble when I placed other includes above httplib.h, are you compiling a server or a client? because for a client I had to link with these: -lcrypto -lssl -lcrypt32 -lws2_32

All I can tell you at this point is that it does compile on Windows, even with Ming64 C++ 8.x, now I am using the latest, one, but it should compile with yours too. The error messages will lead to the solution, the first ones in particular, at least that has been my experience from recent days, I started this month using this terrific module and also had a lot of errors, but most were derived from paths, order of includes and libraries not found (the same on Linux), all solved now.

Good luck, Martin