2015-10-03 20 views
5

Có thể đăng "dữ liệu biểu mẫu" với phần còn lại của C++ SDK (Casablanca) không? Tôi có một dịch vụ web cụ thể tìm kiếm dữ liệu bài đăng trong "dữ liệu biểu mẫu", chứ không phải trong nội dung.C++ phần còn lại sdk Dữ liệu biểu mẫu POST json

Đây là C++:

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()); 
}) 

Các dịch vụ web chỉ có thể sử dụng dữ liệu như thế này (tôi không thể sửa đổi nó):

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

(Đây chỉ là một mẫu mã PHP , những gì tôi sử dụng để thử nghiệm)

Trả lời

4

Đó là cách:

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); 
Các vấn đề liên quan