POST request to a friendly URL fails - c++

I'm trying to post an HTTP request to an HTML form that is accessible through the friendly URL address.
But when I post it, library returns error code "bad URL provided".
HINTERNET hRequest = HttpOpenRequest(hConn, L"POST", L"newform",
NULL, L"http://www.example.com/add/newform/",
rgpszAcceptTypes, NULL, NULL);
DWORD error_code = GetLastError();
bool hResult = HttpSendRequestW(hRequest,NULL,NULL,NULL,NULL);
error_code = GetLastError(); //12005 - bad URL provided
What am I doing wrong, why does my request fails?

The server name (www.site.com in your above example) must be specified in the call to InternetConnect, as must the protocol (INTERNET_SERVICE_HTTP).
In the call to HttpOpenRequest you only give the name of the "object", which in this case is "/add/newform/".
For example (added for more clarity):
HINTERNET hConn = InternetConnect(hInternet, L"www.site.com", INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
HINTERNET hRequest = HttpOpenRequest(hConn, L"POST", L"/add/newform", NULL, NULL,
rgpszAcceptTypes, NULL, NULL);

Related

Bad Request: message text is empty when sending get request via winapi to telegram bot

I'm trying to send message to telegram chat from bot using winapi and c++.
Here is my code
char szData[1024];
// initialize WinInet
HINTERNET hInternet = ::InternetOpen(TEXT("WinInet Test"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet != NULL)
{
// open HTTP session
HINTERNET hConnect = ::InternetConnect(hInternet, L"api.telegram.org", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, 1);
if (hConnect != NULL)
{
wstring request = L"/bot<bot_id>/sendMessage";
// open request
HINTERNET hRequest = ::HttpOpenRequest(hConnect, L"GET", (LPCWSTR)request.c_str(), NULL, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE, 1);
if (hRequest != NULL)
{
// send request
const wchar_t* params = L"?chat_id=<chat_id>&text=test";
BOOL isSend = ::HttpSendRequest(hRequest, NULL, 0, (LPVOID)params, wcslen(params));
if (isSend)
{
for (;;)
{
// reading data
DWORD dwByteRead;
BOOL isRead = ::InternetReadFile(hRequest, szData, sizeof(szData) - 1, &dwByteRead);
// break cycle if error or end
if (isRead == FALSE || dwByteRead == 0)
break;
// saving result
szData[dwByteRead] = 0;
}
}
// close request
::InternetCloseHandle(hRequest);
}
// close session
::InternetCloseHandle(hConnect);
}
// close WinInet
::InternetCloseHandle(hInternet);
}
wstring answer = CharPToWstring(szData);
return answer;
But I've got {"ok":false,"error_code":400,"description":"Bad Request: message text is empty"} response. <chat_id> is id consisted of digits(12345678).
If I run this request in postman or in browser - then everything is ok.
I also tried to run this request using WinHttp* methods and result is the same.
What should I change in my request parameters to make it work?
There are a number of issues with this code:
You don't need to typecast the return value of wstring::c_str() to LPCWSTR (aka const wchar_t*), as it is already that type.
You can't send body data in a GET request. The Telegram Bot API expects body data to be sent in a POST request instead.
You are telling HttpSendRequest() to send body data from a wchar_t* UTF-16 string, but that is not the correct encoding that the server is expecting. You need to use a char* UTF-8 string instead.
You are not sending a Content-Type request header to tell the server what the format of the body data is. The API supports several different formats. In this case, since you are sending the data in application/x-www-form-urlencoded format, you need to add a Content-Type: application/x-www-form-urlencoded header to the request.
With all of that said, try this instead:
// initialize WinInet
HINTERNET hInternet = ::InternetOpenW(L"WinInet Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet == NULL) ... // error handling
// open HTTP session
HINTERNET hConnect = ::InternetConnectW(hInternet, L"api.telegram.org", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, 1);
if (hConnect == NULL) ... // error handling
// open request
wstring wsResource = L"/bot<bot_id>/sendMessage";
HINTERNET hRequest = ::HttpOpenRequestW(hConnect, L"POST", wsResource.c_str(), NULL, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE, 1);
if (hRequest == NULL) ... // error handling
// send request
string sBody = u8"chat_id=<chat_id>&text=test";
BOOL isSend = ::HttpSendRequestW(hRequest, L"Content-Type: application/x-www-form-urlencoded", -1L, sBody.c_str(), sBody.size());
if (!isSend) ... // error handling
string sReply;
char szData[1024];
DWORD dwByteRead;
while (::InternetReadFile(hRequest, szData, sizeof(szData), &dwByteRead) && dwByteRead != 0)
{
// saving result
sReply.append(szData, dwByteRead);
}
...
// use sReply as needed ...

Read response as string wininet

This function creates a post request to an endpoint and receives a JSON response, the goal is to save the json data as a string.
I checked the Microsoft docs but I can't figure it out
void POST(std::string data, std::string endpoint )
{
static TCHAR hdrs[] = _T("Content-Type: application/json");
static LPCSTR accept[2]={"*/*", NULL};
// for clarity, error-checking has been removed
HINTERNET hSession = InternetOpen(DEFAULT_USERAGENT, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hSession, MY_HOST, ALT_HTTP_PORT , NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", endpoint.c_str(), NULL, NULL, accept, 0, 1);
HttpSendRequest(hRequest, hdrs, strlen(hdrs), (PVOID)data.c_str() , data.length());
// close any valid internet-handles
}

HttpSendRequest not posting correct

I am making a simple POST request using WinInet to an apache web server. I encode the data using base 64. The problem is that, every + character gets replaced by space character.
Why is that and how can I make correct POST request.
hInternet = InternetOpen(NULL, INTERNETOPENTYPEPRECONFIG, NULL, NULL, 0);
if (hInternet)
{
hConnect = InternetConnect(hInternet, szDomain, INTERNETDEFAULTHTTPPORT, NULL, NULL, INTERNETSERVICEHTTP, 0, dwTmp);
if (hConnect)
{
hRequest = HttpOpenRequest(hConnect, szPost, szExfiltrationURL, NULL, NULL,(char *)accept, INTERNETFLAGNOCACHEWRITE | INTERNETFLAGNOCOOKIES | INTERNETFLAGNOUI | INTERNETFLAGRELOAD, 0);
if (hRequest)
{
HttpSendRequest(hRequest, headers, lstrlen(headers), buffer, buflen);
InternetCloseHandle(hRequest);
}
InternetCloseHandle(hConnect);
}
InternetCloseHandle(hInternet);
}
From W3
"Within the query string, the plus sign is reserved as shorthand
notation for a space:
Source: "http://www.w3.org/Addressing/URL/4_URI_Recommentations.html"

Send JSON data via HttpOpenRequest

I'm not programming at C++, but i'm asking for someone who does, so i'm sorry if my question is simple or stupid.
I need a simple example of using HttpOpenRequest/HttpSendRequest objects in order to send JSON data to some web service/site.
Thank you
Here is a very bare bones example to send a JSON string to http://hostname/path/scriptname. You will have to add proper error checking, status code checking, etc as needed:
HINTERNET hInternet = InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnect = InternetConnect(hInternet, _T("hostname"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
LPTSTR rgpszAcceptTypes[] = {_T("application/json"), NULL};
HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/path/scriptname"), NULL, NULL, rgpszAcceptTypes, 0, 0);
HttpAddRequestHeaders(hRequest, _T("Content-Type: application/json\r\n"), -1, HTTP_ADDREQ_FLAG_ADD);
char *JsonData = "..."; // your actual JSON data here
HttpSendRequest(hRequest, NULL, 0, JsonData, strlen(JsonData))
DWORD StatusCode = 0;
DWORD StatusCodeLen = sizeof(StatusCode);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &StatusCode, &StatusCodeLen, NULL);
if (StatusCode == 200)
{
// use InternetQueryDataAvailable() and InternetReadFile()
// to read any response data as needed...
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);

Purpose of WINHTTP_QUERY_URI in WinHTTP?

What HTTP header exactly can be queried using WINHTTP_QUERY_URI flag with WinHttpQueryHeaders function? After reading its description I was under impression this flag was supposed to be used to get the URI of the request specified in WinHttpOpenRequest function. Yet the following program gives me an error code 12019 ERROR_INTERNET_INCORRECT_HANDLE_STATE (and 12150 ERROR_HTTP_HEADER_NOT_FOUND if I uncomment two commented lines).
#include <cstdio>
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
HINTERNET hSession = WinHttpOpen(nullptr, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
HINTERNET hConnect = WinHttpConnect(hSession, L"www.ietf.org", INTERNET_DEFAULT_HTTP_PORT, 0);
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/rfc/rfc2616.txt", nullptr, WINHTTP_NO_REFERER, nullptr, 0);
//WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
//WinHttpReceiveResponse(hRequest, 0);
wchar_t url[1024] = {};
DWORD url_size = sizeof(url);
auto success = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_URI, WINHTTP_HEADER_NAME_BY_INDEX, url, &url_size, WINHTTP_NO_HEADER_INDEX);
auto error_code = GetLastError();
wprintf(L"success=%d error_code=%u url=%s", success, error_code, url);
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
}
P.S. Yes, I know I can get request URI using WinHttpQueryOption and WINHTTP_OPTION_URL, no need to point that out.
EDIT. Adding WINHTTP_QUERY_FLAG_REQUEST_HEADERS flag as per Captain Obvlious answer below (which totally makes sense if WINHTTP_QUERY_URI was indeed supposed to return request URI) did not make much difference: now with or without WinHttpSendRequest and WinHttpReceiveResponse calls WinHttpQueryHeaders function produces error code 12150 ERROR_HTTP_HEADER_NOT_FOUND.
You are querying the response headers of the request which do not contain the URI. You need to include the WINHTTP_QUERY_FLAG_REQUEST_HEADERS modifier flag to retrieve from the request header.
WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_URI | WINHTTP_QUERY_FLAG_REQUEST_HEADERS,
WINHTTP_HEADER_NAME_BY_INDEX,
url,
&url_size,
WINHTTP_NO_HEADER_INDEX);