1

I want to add a parameter, but I also have a body, I want to form this format:

HttpPost

http://localhost:8080/master/public/api/v1/invoice/send?token=123456

Currently I have:

HttpPost
http://localhost:8080/master/public/api/v1/invoice/send

 private readonly string UrlBase = "http://localhost:8080";
 private readonly string ServicePrefix = "master/public/api";



public async Task<DocumentResponse> SendInvoice<T>(Invoice body)
            {
            string controller = "/v1/invoice/send";
            try
            {

                var request = JsonConvert.SerializeObject(body);

                var content = new StringContent(
                    request, Encoding.UTF8,
                    "application/json");
                var client = new HttpClient();



                client.BaseAddress = new Uri(UrlBase);
                var url = string.Format("{0}{1}", ServicePrefix, controller);
                var response = await client.PostAsync(url, content);
                Debug.WriteLine(response);
                var result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return new DocumentResponse
                    {

                    };
                }
                var list = JsonConvert.DeserializeObject<DocumentResponse>(result);
                return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                return new DocumentResponse
                {

                };
            }
        }

When I add it directly to the url, the request fails.

Advance

When I add it directly to the url, the request fails. Inquiring about HttpClient, I found this, but how do I add it having a body?

var parameters = new Dictionary<string, string> { { "token", "123456" } };
var encodedContent = new FormUrlEncodedContent (parameters);

Ref: C#: HttpClient with POST parameters

Thank you

2
  • That's definitely doable. You can search for [FromBody] and [FromQuery] for more details. FromBody maps with the body of your request and FromQuery maps with the query string parameters. Commented Nov 18, 2019 at 4:35
  • Disclaimer: I am the author of the library, you can use RetroCoreFit, github.com/neurospeech/asp-net-core-extensions/blob/master/… which gives you better control over communicating for REST api Commented Nov 18, 2019 at 4:59

1 Answer 1

1

When you format your URL you need to add the parameters like so:

var url = string.Format("{0}{1}", ServicePrefix, controller);

url = string.Format("{0}?token=123456", url);

Note the ? between the URL and the query parameter.

You don't specify how you get the value for token into your method but, if it is a readonly value similar to ServicePrefix you can pass it as a parameter to string.Format:

var url = string.Format("{0}{1}", ServicePrefix, controller);

url = string.Format("{0}?token={1}", url, Token);

You can always put this on one line, but I have split it to make it easier to read :-)

Sign up to request clarification or add additional context in comments.

Comments

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.