Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert curl request

Convert curl request

Scheduled Pinned Locked Moved C / C++ / MFC
jsonc++questionlearning
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Flaviu

    I need to do the following request within a C++ app (response is json):

    curl --header "x-api-key:ABCD" -s https://api.test.se/api/mydata

    1. Can I replace curl with another protocol ? (This is not mandatory, could be as it is now) 2. Is there a header only c++ lib that can complete this request ? Of course, I know there is REST SDK library, but it is too much trouble for that simple request. Thank you.

    K Offline
    K Offline
    k5054
    wrote on last edited by
    #2

    curl isn't a protocol, as such, but a CLI tool for data transfer using urls. The underpinnings are based on libcurl, a C library, that implements the data trasfer. As such, you can make API calls directly from your C++ program. Another option would be to look into popen() e.g.

    #include

    int main()
    {
    FILE *pipe = popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");

    while( // read data from **pipe** is true )
    {
         // process data
    }
    fclose(pipe);
    

    }

    Keep Calm and Carry On

    _ 1 Reply Last reply
    0
    • K k5054

      curl isn't a protocol, as such, but a CLI tool for data transfer using urls. The underpinnings are based on libcurl, a C library, that implements the data trasfer. As such, you can make API calls directly from your C++ program. Another option would be to look into popen() e.g.

      #include

      int main()
      {
      FILE *pipe = popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");

      while( // read data from **pipe** is true )
      {
           // process data
      }
      fclose(pipe);
      

      }

      Keep Calm and Carry On

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #3

      Hmm ... I tried:

      FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");

      while (true)
      {
      	pipe->\_Placeholder;
      	break;
      }
      
      fclose(pipe);
      

      but I cannot retrieved any data from pipe ...

      L K 2 Replies Last reply
      0
      • _ _Flaviu

        Hmm ... I tried:

        FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");

        while (true)
        {
        	pipe->\_Placeholder;
        	break;
        }
        
        fclose(pipe);
        

        but I cannot retrieved any data from pipe ...

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #4

        using a C style FILE * you have to use C stdio functions. So for example you could do

        #include
        #include

        FILE *pipe = popen( ... );

        char *buff = NULL;
        size_t blen = 0;
        while( getline(&buff, &blen, pipe) > 0) {
        // process input data in buff;
        }
        free(buff);

        I generally prefer getline() to fgets() when using cstdio because it allocates and grows the input buffer as needed. If you wish to stick with a more C++ style interface, then maybe look into [Boost.Process](https://www.boost.org/doc/libs/1\_79\_0/doc/html/process.html) I think you've mentioned QT as part of your framework, so there's [QProcess](https://doc.qt.io/qt-6/qprocess.html) as well.

        Keep Calm and Carry On

        _ 1 Reply Last reply
        0
        • _ _Flaviu

          Hmm ... I tried:

          FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");

          while (true)
          {
          	pipe->\_Placeholder;
          	break;
          }
          
          fclose(pipe);
          

          but I cannot retrieved any data from pipe ...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          The pipe returned from _popen is a FILE* so you must read the date from it.

          1 Reply Last reply
          0
          • K k5054

            using a C style FILE * you have to use C stdio functions. So for example you could do

            #include
            #include

            FILE *pipe = popen( ... );

            char *buff = NULL;
            size_t blen = 0;
            while( getline(&buff, &blen, pipe) > 0) {
            // process input data in buff;
            }
            free(buff);

            I generally prefer getline() to fgets() when using cstdio because it allocates and grows the input buffer as needed. If you wish to stick with a more C++ style interface, then maybe look into [Boost.Process](https://www.boost.org/doc/libs/1\_79\_0/doc/html/process.html) I think you've mentioned QT as part of your framework, so there's [QProcess](https://doc.qt.io/qt-6/qprocess.html) as well.

            Keep Calm and Carry On

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #6

            The C++ app is MFC app ...

            K 1 Reply Last reply
            0
            • _ _Flaviu

              The C++ app is MFC app ...

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #7

              For some reason, I was assuming you were using linux. If you're on windows, there's no getline in the C stdio library, so you'll have to use fgets() or another C stdio FILE i/o function. It might be a better choice to use the libcurl API directly. Or another C/C++ client library. A quick google search turned up [Windows HTTP Services - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttp-start-page) , but that appears to be a Win32 API, so may not be applicable to your situation. There's also [GitHub - embeddedmz/httpclient-cpp: C++ client for making simple HTTP requests](https://github.com/embeddedmz/httpclient-cpp) which might work for you too. I have not tried this package, so cannot speak to its quality or suitability for any purpose, what so ever. Caveat usor.

              Keep Calm and Carry On

              _ 1 Reply Last reply
              0
              • K k5054

                For some reason, I was assuming you were using linux. If you're on windows, there's no getline in the C stdio library, so you'll have to use fgets() or another C stdio FILE i/o function. It might be a better choice to use the libcurl API directly. Or another C/C++ client library. A quick google search turned up [Windows HTTP Services - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttp-start-page) , but that appears to be a Win32 API, so may not be applicable to your situation. There's also [GitHub - embeddedmz/httpclient-cpp: C++ client for making simple HTTP requests](https://github.com/embeddedmz/httpclient-cpp) which might work for you too. I have not tried this package, so cannot speak to its quality or suitability for any purpose, what so ever. Caveat usor.

                Keep Calm and Carry On

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #8

                But there is getline in c++: std::getline - cppreference.com[^] I need to try it, then I'll ocme back wih feedback.

                K 1 Reply Last reply
                0
                • _ _Flaviu

                  But there is getline in c++: std::getline - cppreference.com[^] I need to try it, then I'll ocme back wih feedback.

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #9

                  std::getline() isn't part of C stdio, its part of C++. The getline with the signature int getline(char **, size_t *, FILE *) (i.e C stdio, thus the FILE * parameter) is not provided by MS in the windows C/C++ environment. Or at least it wasn't with VS 2017. It might have been added since then, but I don't think so.

                  Keep Calm and Carry On

                  _ 1 Reply Last reply
                  0
                  • K k5054

                    std::getline() isn't part of C stdio, its part of C++. The getline with the signature int getline(char **, size_t *, FILE *) (i.e C stdio, thus the FILE * parameter) is not provided by MS in the windows C/C++ environment. Or at least it wasn't with VS 2017. It might have been added since then, but I don't think so.

                    Keep Calm and Carry On

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #10

                    I have tried:

                    std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
                    while (ifs.good())
                    {
                    std::string line;
                    while (std::getline(ifs, line))
                    {
                    std::cout << line.c_str() << std::endl;
                    }
                    }

                    Nothing retrieved.

                    L K 2 Replies Last reply
                    0
                    • _ _Flaviu

                      I have tried:

                      std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
                      while (ifs.good())
                      {
                      std::string line;
                      while (std::getline(ifs, line))
                      {
                      std::cout << line.c_str() << std::endl;
                      }
                      }

                      Nothing retrieved.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      std::ifstream reads files, and does not have a constructor which executes an external program. You should go back to pure C and use the _popen function. [edit] This all you need:

                      char buffer\[132\];
                      FILE\* pipette = \_popen("curl --header \\"x-api-key:ABCD\\" -s https://api.test.se/api/mydata", "r");
                      while (fgets(buffer, 132, pipette))
                      {
                          printf("%s", buffer);
                      }
                      

                      [/edit]

                      _ 1 Reply Last reply
                      0
                      • _ _Flaviu

                        I have tried:

                        std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
                        while (ifs.good())
                        {
                        std::string line;
                        while (std::getline(ifs, line))
                        {
                        std::cout << line.c_str() << std::endl;
                        }
                        }

                        Nothing retrieved.

                        K Offline
                        K Offline
                        k5054
                        wrote on last edited by
                        #12

                        Of course not, unless you actually have a file in your current directory called curl --header "x-api-key:ABCD" -s https://api.test.se/api/mydata.

                        Keep Calm and Carry On

                        1 Reply Last reply
                        0
                        • L Lost User

                          std::ifstream reads files, and does not have a constructor which executes an external program. You should go back to pure C and use the _popen function. [edit] This all you need:

                          char buffer\[132\];
                          FILE\* pipette = \_popen("curl --header \\"x-api-key:ABCD\\" -s https://api.test.se/api/mydata", "r");
                          while (fgets(buffer, 132, pipette))
                          {
                              printf("%s", buffer);
                          }
                          

                          [/edit]

                          _ Offline
                          _ Offline
                          _Flaviu
                          wrote on last edited by
                          #13

                          Yes, that seems to work. Thank you all of you !

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups