txizzle
txizzle

Reputation: 840

C# HttpClient PostAsync with JSON parameter for VSO git API

I am trying to understand the VSO git API. I have made Get requests succesfully like so:

using (var response = client.GetAsync(
                        uri).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        var responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                        ResponseInfo.Text = JsonHelper.FormatJson(responseBody);
                    }

I do this after setting client.DefaultRequestHeaders for Basic Authentication and Mediatype to application/json.

For post requests, the VSO Documentation shows this: enter image description here

I understand that the parameters are JSON. However, I'm not sure how to pass that into the post request in C#. I have tried the following:

string content = @"{
                              ""refUpdates"": [
                                {
                                  ""name"": ""refs/heads/master"",
                                  ""oldObjectId"": ""*old object id*""
                                }
                              ],
                              ""commits"": [
                                {
                                  ""comment"": ""Test commit"",
                                  ""changes"": [
                                    {
                                      ""changeType"": ""edit"",
                                      ""item"": {
                                        ""path"": ""/foo.txt""
                                      },
                                      ""newContent"": {
                                        ""content"": ""test"",
                                        ""contentType"": ""rawtext""
                                      }
                                    }
                                  ]
                                }
                              ]
                            }";

            var stringToJson= new JavaScriptSerializer();
            var JSONoutput = stringToJson.Deserialize<object>(content);
            StringContent stringContent = new StringContent(JSONoutput.ToString(), Encoding.UTF8, "application/json");

and then I pass that in to

using (var response = client.PostAsync(uri, stringContent).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        var responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }

I get a 400 Bad Request error. Am I passing in my parameters correctly? Essentially I am taking the string version of what the tutorial gave me, convert it to JSON, deserialize it, convert it to HTTPContent, and pass that into PostAsync. I can't think of another way to do it.

Thank you for your time!

Upvotes: 2

Views: 1583

Answers (1)

txizzle
txizzle

Reputation: 840

Turns out I can just do

 StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/json");

The string version of the JSON object is enough for StringContent.

Upvotes: 1

Related Questions