C ++ rest sdk post form data json

Is it possible to publish "form data" with the C ++ rest SDK (Casablanca)? I have a specific web service that searches for data to be published in "form data" and not in the body.

This is the C ++ code:

http_client client(L"http://localhost/posttest/jsontest.php"); // Manually build up an HTTP request with header and request URI. http_request request(methods::POST); request.headers().add(L"Content-Type", L"application/json"); request.headers().add(L"Content-Length", L"100"); request.headers().add(L"Host", L"example.com"); request.headers().add(L"X-Requested-With", L"XMLHttpRequest"); request.set_body(obj); return client.request(request).then([id](http_response response) { if (response.status_code() == status_codes::OK) { return response.extract_json(); } else { /* Print bad status code */ wcout << L"Server returned returned status code " << response.status_code() << L'.' << std::endl; } return pplx::task_from_result(json::value()); }) 

The web service can only use such data (I cannot change it):

 $arr = [$_POST['code']]; header('Content-Type: application/json'); echo json_encode($arr); 

(This is just a sample of the PHP code that I use for testing)

+5
source share
1 answer

Like this:

 utility::string_t Lreq = L"code=" + Lcode; http_client client(L"http://localhost/posttest/jsontest.php"); // Manually build up an HTTP request with header and request URI. http_request request(methods::POST); request.headers().add(L"Content-Type", L"application/x-www-form-urlencoded; charset=UTF-8"); request.headers().add(L"Content-Length", L"100"); request.headers().add(L"Host", L"testhost.com"); request.headers().add(L"X-Requested-With", L"XMLHttpRequest"); request.set_body(Lreq); 
+4
source

Source: https://habr.com/ru/post/1232860/


All Articles