0

Currently I have 3rd party WebApi which has the following external call using Flurl builder.

 await _client.Url.ToString().PostJsonAsync(data);

And I'm trying to handle the response with such endpoint:

[HttpPost]
public void HandleResponse(HttpResponseMessage response)
{
}

The response message is with status OK but has Content and Headers = null

How can I handle this properly?

Stanimir Yakimov
  • 864
  • 3
  • 14
  • 31
  • Are you asking about how to call an async method from a sync context? – Rubens Farias May 19 '17 at 11:04
  • The opposite, I want to create an endpoint to handle the `client` request – Stanimir Yakimov May 19 '17 at 11:05
  • I think you're confusing *requests* with *responses*. Shouldn't that be an `HttpRequestMessage` instead? And shouldn't your API *return* a response? – David May 19 '17 at 11:06
  • The response to your endpoint has an empty body because the endpoint has a `void` return type and doesn't do anything whatsoever. Does that answer your question? – Todd Menier May 19 '17 at 12:47
  • Given the semantic changes you've made to the code in the question, it's not really clear what you're even building at this point. The first code snippet is making an HTTP request to some resource. What is the second code snippet? How does it relate to the first? – David May 19 '17 at 15:21
  • @David First code snippet is actually calling the second snippet (the endpoint). But currently in the response there's no content and headers as expected – Stanimir Yakimov May 19 '17 at 15:58
  • @StanimirYakimov: The semantics of this are very important. *Requests* and *responses* are two very different things. If the second code snippet is the API endpoint being invoked by the first, then it would be *handling a request* and *returning a response*. Currently you're trying to *handle a response* (which makes no sense) and *return nothing* (which doesn't make a whole lot of sense either). – David May 19 '17 at 16:00
  • @David That's my question. if the `PostJsonAsync()` has the following signature `public static Task PostJsonAsync(this string url, object data);` then what will be the parameter type in the endpoint – Stanimir Yakimov May 19 '17 at 16:11

1 Answers1

0

This API endpoint doesn't make any sense to me:

[HttpPost]
public void HandleResponse(HttpResponseMessage response)
{
    //...
}

The endpoint would be handling a request and returning a response, not the other way around. Something more like this:

[HttpPost]
public HttpResponseMessage HandleResponse(HttpRequestMessage request)
{
    //...
}

When something contacts an API (or server of any kind, really), it's sending a request to that API. That API receives the request and returns a response. The semantics of these two words pretty much describe the process, it's very important to keep them straight.

Consider it like this... If someone asks you a question, what you receive is a question. Not an answer. What you send back to that person is the answer.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks. I've just changed the parameter to `HttpRequestMessage` but it seems request that's coming is empty with `null` Content and Headers – Stanimir Yakimov May 19 '17 at 16:39