0

I'm trying to post a data from a Web Api to another Web Api in different project solution with a controller like this

public class PublishWebController : ApiController
{
    static HttpClient client = new HttpClient();

    static async Task<Uri> CreateArticleAsync(Article article)
    {
        HttpResponseMessage response = await client.PostAsJsonAsync("api/articles", article);
        response.EnsureSuccessStatusCode();

        // return URI of the created resource.
        return response.Headers.Location;
    }

    void Main()
    {
        Post().GetAwaiter().GetResult();
    }

    //POST api/<controller>
    [Mime]
    public async Task<Article> Post()
    {

        client.BaseAddress = new Uri("http://localhost:64395/api/");
        client.DefaultRequestHeaders.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var fileuploadPath = HttpContext.Current.Server.MapPath("~/uploads");
        var multiFormDataStreamProvider = new MultiFileUploadProvider(fileuploadPath);
        await Request.Content.ReadAsMultipartAsync(multiFormDataStreamProvider);
        string uploadingFileName = multiFormDataStreamProvider.FileData.Select(x => x.LocalFileName).FirstOrDefault();

        return new Article
        {
            Title = HttpContext.Current.Request.Form["Title"],
            ArticleContent = HttpContext.Current.Request.Form["ArticleContent"],
            ImagePath = "~/Images/" + Path.GetFileName(uploadingFileName),
        };

    }
}

When I tried sending a data with Postman, it returned a value to its localhost but nothing to the uri that I targeted

sending data

uri target

How do I fix this problem?

1 Answer 1

1

Please check if the uri specified is proper.

Please find that :

client.BaseAddress = new Uri("http://localhost:64395/api/");

and await client.PostAsJsonAsync("api/articles", article);

gives us: http://localhost:64395/api/api/articles

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.