Skip to content

HttpBody field causes problems in client libraries #1068

Closed
@chrisdunelm

Description

@chrisdunelm

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:

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:

  1. 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();
  1. 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;

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions