skip to Main Content

I don’t have experience with REST APIs but I inherited one that I need to develop a C# .NET Framework 4.71-based client for.
I figured out how to invoke commands that don’t need parameters. For example, I found this definition in the server (a C# .NET Framework 4.71 WPF GUI app which uses swagger):

  • openapi-full.json
"openapi": "3.0.1",
...
"paths": {

"/api/v1/Run/New": {
   "post": {
      "summary": "...",
      "description": "...",
      "tags": [
         "..",
         ".."
      ],
      "responses": {...}
   }
},

and found I could invoke it from the client I’m developing using:

HttpResponseMessage response = await client.PostAsJsonAsync("api/v1/Run/New", new object());

I get lost when the command requires a parameter:

"/api/v1/Tests": {         
   "put": {
      "summary": "...",
      "description": "...",
      "tags": [
         "...",
         "..."
      ],
      "requestBody": {
         "content": {
            "multipart/form-data": {
               "schema": { "$ref": "#/components/schemas/ListOfTestIds" }
            }
         }
      },
      "responses": {...}
   }
}

How would I pass a list of ints? More importantly, is there guidance online for translating requestBody syntax to PostAsJsonAsync parameters?

2

Answers


  1. You can use the openApi-generator to generate a Class-Library out of the openapi-full.json file.

    After installing you should be able to generate the library like this:

    openapi-generator generate -i openapi-full.json -g csharp -o ./output
    

    Take the command with a grain of salt, it has been a while since i last used the openApi Generator.

    Login or Signup to reply.
  2. For your first question, client.PutAsJsonAsync("/api/v1/Tests", **reference to the int[] here**) should work for what you intend (note that the verb is PUT for that endpoint).

    As for your second question, a good answer as @Sebastian put it, is the openAPI-generator. Several topics on it already exist, such as this answer.

    EDIT: somehow missed that the PUT endpoint content is multipart/form-data – PutAsJsonAsync might not cut it here, in which case this thread could be helpful.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search