Halo jak napisać rozkaz który pobierze dane z coinmarketcap.com API bez użycia jakiś dodatkowych bibliotek
Program jest napisany prawidłowo ale dostaję ten błąd
error:
<center><h1>400 Bad Request</h1></center>
Nie wiem co mam napisać w zmiennej, szukałem ale nigdzie nie znalazłem jak powinny wyglądać headery API:
string get_http =
dokumentacja:
//https://coinmarketcap.com/api/documentation/v1/#
//https://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c
//http://www.cplusplus.com/forum/beginner/139313/
//https://aticleworld.com/http-get-and-post-methods-example-in-c/
//https://docs.microsoft.com/en-us/windows/desktop/api/wininet/nf-wininet-httpsendrequesta
//You can supply your API Key in REST API calls in one of two ways:
// Preferred method: Via a custom header named X-CMC_PRO_API_KEY
// Convenience method: Via a query string parameter named CMC_PRO_API_KEY
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib");
#pragma comment(lib,"libws2_32.a");
#pragma comment(lib,"lws2_32.a");
int main( void ){
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount=0;
int rowCount=0;
struct hostent *host;
locale local;
char buffer[10000];
int i = 0 ;
int nDataLength;
string website_HTML;
// website url
//string url = "pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
string key22 = "x";
string url5 = "pro-api.coinmarketcap.com";
//HTTP GET
string get_http = "GET /v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY="+key22+" HTTP/1.1\r\n'Host': pro-api.coinmarketcap.com\r\nConnection: close\r\n\r\n";
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
cout << "WSAStartup failed.\n";
system("pause");
//return 1;
}
Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
host = gethostbyname(url5.c_str());
cout<<"1"<<endl;
//system("pause");
SockAddr.sin_port=htons(80);
cout<<"2"<<endl;
//system("pause");
SockAddr.sin_family=AF_INET;
cout<<"22"<<endl;
//system("pause");
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout<<"3"<<endl;
//system("pause");
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
//return 1;
}
cout<<"4"<<endl;
//system("pause");
// send GET / HTTP
send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 );
//send(Socket,get_http2.c_str(), strlen(get_http2.c_str()),0 );
//send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 );
cout<<"5"<<endl;
// system("pause");
// recieve html
while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){
//cout<<"7"<<endl;
//system("pause");
website_HTML+=buffer[i];
i += 1;
}
}
cout<<"8"<<endl;
// system("pause");
closesocket(Socket);
WSACleanup();
cout<<"9"<<endl;
//system("pause");
// Display HTML source
cout<<website_HTML;
// pause
cout<<"\n\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}