Click here to Skip to main content
15,795,496 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a Webservice. This webservice should call an other Endpoint from different services. That means, the Webservice should work like a Proxy.

I realize all GET Methods and one POST Method. But the last POST Method don't work. It is different to the first method.

In the first Method i get a file (datatype IFormFile) in the Body of the Post-Request as form-data. The response is the Status OK. That works.

In the second Method i get also such a file and some additional information (datatype string and int) in the Body of the Post-Request as form-data. But that doesn't work.

I have no error in the code itself. I have tried it out with Postman, load all the data as form-data in the body. I send the data and see in the logs the Post, but i need the response, which came back and there is no data. How should i do this.

That, what i posted, is the code right know. There you got a statuscode. But i don't need a statuscode. I need a responseBody. I have tried it with HttpContent and HttpResponseMessage. Both are empty.

What i do wrong?

What I have tried:

[HttpPost("Test")]
public StatusCodeResult trace(IFormFile file)
{
    try
    {
        if (file != null && file.Length > 0)
        {
            using (var client = new HttpClient())
            {
                try
                {
                    client.BaseAddress = new Uri("https://localhost:40111");

                    byte[] data;
                    using (var br = new BinaryReader(file.OpenReadStream()))
                        data = br.ReadBytes((int)file.OpenReadStream().Length);
                    ByteArrayContent bytes = new ByteArrayContent(data);

                    MultipartFormDataContent multiContent = new MultipartFormDataContent();

                    multiContent.Add(bytes, "image", file.FileName);
                    multiContent.Add(new StringContent("132"), "itemId");
                    var result = client.PostAsync("/test", multiContent).Result;
                    HttpContent response = result.Content;
                    return StatusCode((int)result.StatusCode); //201 Created the request has been fulfilled, resulting in the creation of a new resource.

                }
                catch (Exception)
                {
                    return StatusCode(500); // 500 is generic server error
                }
            }
        }
        return StatusCode(400); // 400 is bad request
    }
    catch (Exception)
    {
        return StatusCode(500); // 500 is generic server error
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900