Closed
Description
Some libraries (e.g. Cloud Machine Learning Engine) require un-typed JSON to be passed through to the servre; ignoring the strongly-typed generated code.
There is no obvious way to do this, leading to users having problems:
- https://stackoverflow.com/questions/43638529/google-cloud-machine-learning-api-dot-net-client-predict-request-failing
- https://stackoverflow.com/questions/43805736/google-machine-learning-api-issue
We might want to change the code generator to make this simple to use for APIs that require this behaviour.
There are two known work-arounds, using CMLE as an example:
- Use the
ModifyRequest
hook on the service request:
// Create the underlying service. In real use this will probably require authentication
CloudMachineLearningEngineService service = new CloudMachineLearningEngineService();
// Prepare to send the request
string actualBodyJson = "Request JSON";
GoogleCloudMlV1PredictRequest emptyBody = new GoogleCloudMlV1PredictRequest { HttpBody = new GoogleApiHttpBody() };
ProjectsResource.PredictRequest predictRequest = service.Projects.Predict(emptyBody, "projects/xyz");
predictRequest.ModifyRequest = httpRequestMessage =>
{
httpRequestMessage.Content = new StringContent(actualBodyJson, Encoding.UTF8, "application/json");
};
// Send the request with the body set to 'actualBodyJson'
GoogleApiHttpBody predictResponse = predictRequest.Execute();
- Using the service
HttpClient
directly:
// Create the underlying service. In real use this will probably require authentication
CloudMachineLearningEngineService service = new CloudMachineLearningEngineService();
// Prepare to send the request
string actualBodyJson = "Request JSON";
GoogleCloudMlV1PredictRequest emptyBody = new GoogleCloudMlV1PredictRequest { HttpBody = new GoogleApiHttpBody() };
ProjectsResource.PredictRequest predictRequest = service.Projects.Predict(emptyBody, "projects/xyz");
Uri uri = predictRequest.CreateRequest().RequestUri;
HttpContent content = new StringContent(actualBodyJson, Encoding.UTF8, "application/json");
HttpResponseMessage predictResponse = service.HttpClient.PostAsync(uri, content).Result;