0

I am trying to post a specific HTTP Post however i am not understanding the conversion for C# to do this correctly, I feel like I am most of the way there but getting stuck along the way

This is what I am trying to send:

curl -i -X POST -H 'Content-Type: application/json' -d '{"text": " Some text or a string in my case a string :tada:"}' http://example.com/hooks/KEYDATA

Should look something like this...

POST /hooks/KEYDATA HTTP/1.1
Host: http://example.com
Content-Type: application/json
Content-Length: 63

Here is what I have....

           async Task sendRequest()
            {
                using var httpClient = new HttpClient();
                using var request = new HttpRequestMessage(new HttpMethod("POST"), 
"https://example.com/hooks/KEYDETAIL");
                request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
                request.Content = new StringContent("{\"text\":\" " + questionCreated + "\" :tada:\"}", 
Encoding.UTF8, "application/json");

                var response = await httpClient.SendAsync(request);
                MessageBox.Show(request.ToString());
            }
2
  • Why are you showing the request in the messagebox instead of the response? Is that intentional? You're not doing anything with the response. Commented Aug 12, 2020 at 21:20
  • In the request.Content you add an additional " directly before :tada: which is not in your curl example. Is this by accident? Commented Aug 12, 2020 at 21:40

2 Answers 2

3

You are trying to display the request instead of the response.

The response is of type HttpResponseMessage

You are displaying the request in the message box, not the response. Assuming you want to see the actual response stream, try the following, it reads the content as string:

MessageBox.Show(await response.Content.ReadAsStringAsync());

See also: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=netcore-3.1

With the HttpResponseMessage you're able to read multiple properties like the headers or status code.


In order to debug such things, try to read the responses content and statuscode properties.

Another tip: I noticed you mannualy constructing a JSON string; using NewtonSoft you should be able to do just this:

string jsonstring = JsonConvert.SerializeObject(new { text = "tada" } ); 

Another tip, for me usualy the following suffies:

using (var client = new HttpClient())
{
    //no need to set header
    var request = new HttpRequestMessage(new HttpMethod("POST"), "https://example.com/hooks/KEYDETAIL")
    {
        Content = new StringContent(JsonConvert.SerializeObject(yourobject),
                                    Encoding.UTF8, "application/json")
    };

     var response = await httpClient.SendAsync(request);
} 
Sign up to request clarification or add additional context in comments.

13 Comments

my messagebox works fine its the post that isnt working. :-(
It would help if you describe in detail what exactly is not working, for example what is the actual response? The status code and response should display information, or the call should throw an exception.
So that answer doesn't resolve the issue. the only response I am getting from the server is that it failed to parse my request. I was showing my request in an effort to see if i was sending the correct data.
@MichaelWilliams: hi, can you post the actual status code and response from the server or exception?
meanwhile, I'll try to recreate it in an actual project
|
1

Your C# code generates a different/invalid json string than you provided in the curl call.

If you take

request.Content = new StringContent("{\"text\":\" " + questionCreated + "\" :tada:\"}", 
    Encoding.UTF8, "application/json");

The resulting string will be (look at the additional " before :tata:).

{"text":" Some text or a string in my case a string" :tada:"}

instead of

{"text": " Some text or a string in my case a string :tada:"}

Replace it with

request.Content = new StringContent("{\"text\":\" " + questionCreated + " :tada:\"}", 
    Encoding.UTF8, "application/json");

1 Comment

actually: nice find :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.