受欢迎的博客标签

C++中调用RESTful api接口上传数据

Published

现在有个项目需要把本地数据库中指定的数据上传到服务器,现在云服务器提供了RestAPI接口,我在VC中如何调用和使用RestAPI.

1.rest不就是json+http嘛,http支持MFC有,找个json库就行

2.微软的cpprestsdk

https://github.com/microsoft/cpprestsdk

 

Environment

OS: Windows 10.0.18362, VS 2019, clean new environment.
Compiler: revision
Microsoft Visual Studio Enterprise 2019 Version 16.4.2

 

 

Visual Studio 2019  c++ Libraries like RestSDK for C++

step 1:  add the REST SDK to a project by using NuGet.

Install-Package cpprestsdk.v.141 -Version 2.10.7
1. right-click References or a project
2. select Manage NuGet Packages…

 

Install packages with VCPKG (C++包管理工具)

install vcpkg from https://github.com/Microsoft/vcpkg
Compile cpprestsdk using following commands:

Step 1. vcpkg install cpprestsdk:x86-windows-static

Step 2. vcpkg install cpprestsdk:x64-windows-static

 

open command window as admin and enter

Vcpkg helps you manage C and C++ libraries on Windows, Linux, and macOS.

vcpkg作为微软开发的一款C++开源代码管理工具,并未随Visual Studio 安装在系统中,需要手动下载该工具源码并进行编译才可以使用.

该工具除了可以下载Cpp Rest SDK源码外还可自动下载相关依赖包,并通过命令行进行自动编译、向VisualStudio引入相关库,大大简化了在自己的项目中使用Cpp Rest SDK的难度。

Vcpkg自动下载源码然后编译成三方库,而且并不依赖于Windows注册表或Visual Studio.

step 1:Clone the VCPKG repo from GitHub

git clone https://github.com/Microsoft/vcpkg

step 2:compile the package manager(运行完成后,会在当前目录下生成vcpkg.exe)

.\bootstrap-vcpkg.bat

output

D:\GoogleDownload\vcpkg-master\vcpkg-master>.\bootstrap-vcpkg.bat
Downloading https://github.com/microsoft/vcpkg-tool/releases/download/2021-09-10/vcpkg.exe -> D:\GoogleDownload\vcpkg-master\vcpkg-master\vcpkg.exe... done.
Validating signature... done.

 

step 3:install the C++ REST SDK on windows

C++ REST SDK can be installed on Windows by using VCPKG

三方库的下载和编译命令是vcpkg install

.\vcpkg install cpprestsdk cpprestsdk:x64-windows
or

PS> vcpkg install cpprestsdk cpprestsdk:x64-windows

output

Computing installation plan...
The following packages will be built and installed:
  * brotli[core]:x64-windows -> 1.0.9#2
  * brotli[core]:x86-windows -> 1.0.9#2
    cpprestsdk[brotli,compression,core,default-features]:x64-windows -> 2.10.18
    cpprestsdk[brotli,compression,core,default-features]:x86-windows -> 2.10.18
  * zlib[core]:x64-windows -> 1.2.11#12
  * zlib[core]:x86-windows -> 1.2.11#12
Additional packages (*) will be modified to complete this operation.
Detecting compiler hash for triplet x64-windows...
A suitable version of git was not found (required v2.32.0). Downloading portable git v2.32.0...
Downloading git...
  https://github.com/git-for-windows/git/releases/download/v2.32.0.windows.2/PortableGit-2.32.0.2-32-bit.7z.exe -> D:\GoogleDownload\vcpkg-master\vcpkg-master\downloads\PortableGit-2.32.0.2-32-bit.7z.exe

 

other:

run failure because of language pack in vs2019

Computing installation plan...
The following packages will be built and installed:
  * brotli[core]:x64-windows -> 1.0.9#2
  * brotli[core]:x86-windows -> 1.0.9#2
    cpprestsdk[brotli,compression,core,default-features]:x64-windows -> 2.10.18
    cpprestsdk[brotli,compression,core,default-features]:x86-windows -> 2.10.18
  * zlib[core]:x64-windows -> 1.2.11#12
  * zlib[core]:x86-windows -> 1.2.11#12
Additional packages (*) will be modified to complete this operation.
Warning: The following VS instances are excluded because the English language pack is unavailable.
    C:\Program Files\Microsoft Visual Studio\2022\Preview
Please install the English language pack.
No suitable Visual Studio instances were found

在Visual Studio Installer中“修改”,点击“语言包”,勾上英语

We can check that cpprestsdk was successfully installed for x86 Windows desktop by running the list command.

PS D:\src\vcpkg> .\vcpkg list

PS D:\GoogleDownload\vcpkg-master\vcpkg-master> .\vcpkg list
brotli:x64-windows                                 1.0.9#2          a generic-purpose lossless compression algorithm
brotli:x86-windows                                 1.0.9#2          a generic-purpose lossless compression algorithm
cpprestsdk:x64-windows                             2.10.18          C++11 JSON, REST, and OAuth library
cpprestsdk:x86-windows                             2.10.18          C++11 JSON, REST, and OAuth library
cpprestsdk[brotli]:x64-windows                                      Brotli compression support
cpprestsdk[brotli]:x86-windows                                      Brotli compression support
cpprestsdk[compression]:x64-windows                                 HTTP Compression support
cpprestsdk[compression]:x86-windows                                 HTTP Compression support
cpprestsdk[default-features]:x64-windows                            Features installed by default
cpprestsdk[default-features]:x86-windows                            Features installed by default
zlib:x64-windows                                   1.2.11#12        A compression library
zlib:x86-windows                                   1.2.11#12        A compression library

step 4:vcpkg integration in Visual Studio  2019 for CMake projects(整合Vcpkg sdk到VS中)

The recommended and most productive way to use vcpkg is via user-wide integration, making the system available for all projects you build. The user-wide integration will prompt for administrator access the first time it is used on a given machine, but afterwards is no longer required and the integration is configured on a per-user basis.

vcpkg integrate install命令将所有三方库的头文件和二进制文件自动配置到Visual Studio中

For usage in Visual Studio you have to enable the system wide integration with

.\vcpkg integrate install

Note: You will need to restart Visual Studio or perform a Build to update intellisense with the changes.

other

//To remove  the vcpkg integration for your user, you can use 
.\vcpkg integrate remove

step 5: include a C++ REST SDK header into your  Visual Studio C++ project

 

 

#include <cpprest/http_listener.h>              // HTTP server
#include <cpprest/json.h>                       // JSON library
#include <cpprest/uri.h>                        // URI library
#include <cpprest/ws_client.h>                  // WebSocket client
#include <cpprest/containerstream.h>            // Async streams backed by STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer scenarios

 

install  C++ REST SDK using nuget

https://social.msdn.microsoft.com/Forums/vstudio/en-US/1b7d3e81-85e5-4ec0-bd60-f424dceab232/visual-studio-2019-libraries-like-restsdk-for-c

 

build CppRestSDK using Vcpkg

How to build CppRestSDK using Vcpkg in Visual Studio 2019 (X64 windows & X86 windows)

http://cool-emerald.blogspot.com/2020/10/c-rest-sdk.html

https://sageai.blogspot.com/2020/12/how-to-build-cpprestsdk-using-vcpkg-in.html

https://vcpkg.readthedocs.io/en/latest/examples/installing-and-using-packages/

https://blog.csdn.net/Lasuerte/article/details/86689703(ok cn)

code

Full-fledged client-server example with C++ REST SDK 2.10