Search code examples
c#jsonpostmanhttpclientdotnet-httpclient

HttpClient post call works on postman but not C#


As the title says, my post-call works in postman but not in c#. No matter what I try, it is simply not working in my c# code.

Here is my code:

internal async Task<(string, int)> GetJsonFileFromApiCall(string query)
    {
        var _client = new HttpClient();
        var apiUrl = "https://wf19vv0nsf-dsn.algolia.net/1/indexes/*/queries?x-algolia-agent=Algolia%20for%20vanilla%20JavaScript%20(lite)%203.27.0%3Binstantsearch.js%202.10.2%3BMagento2%20integration%20(1.12.1)%3BJS%20Helper%202.26.0&x-algolia-application-id=WF19VV0NSF&x-algolia-api-key=MDdmNjA0Mjc1YzRkZjI4MWMwZmQyMDI4MDc5NDY4ZjlkYzJmOTVmMWY5Yjc3MGFkNDRiODA4YjU0MDVlM2Q1YnRhZ0ZpbHRlcnM9";
        var load = new { Requests = new { IndexName = "magento2_tcg_productiondefault_products", Params = $"query={query}&hitsPerPage=12&maxValuesPerFacet=8&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&ruleContexts=%5B%22magento_filters%22%2C%22%22%5D&facets=%5B%22mtg_setcode%22%2C%22is_foil%22%2C%22mtg_rarity%22%2C%22sf_colours%22%2C%22sf_types%22%2C%22sf_legalities%22%2C%22price.AUD.default%22%2C%22stock_qty%22%2C%22mtg_cmc%22%5D&numericFilters=%5B%22visibility_search%3D1%22%5D" } };

        var message = await _client.PostAsync(apiUrl, GetStringContent(load));
        message.EnsureSuccessStatusCode();
        
        var jToken = JObject.Parse(await message.Content.ReadAsStringAsync());
        
        //temp return
        return ("", 1);
    } 

    private static StringContent GetStringContent<T>(T load)
    {
        var serializeObject = JsonConvert.SerializeObject(load, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
        return new StringContent(serializeObject, Encoding.UTF8, "application/json");
    }

Here are my Postman screenshots: Body Headers

One thing to note is that the post request takes in a form data when making the post call.


Solution

  • I think you should change this line of your code

    var load = new { Requests = new { IndexName = "magento2_tcg_productiondefault_products", Params = $"query={query}&hitsPerPage=12&maxValuesPerFacet=8&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&ruleContexts=%5B%22magento_filters%22%2C%22%22%5D&facets=%5B%22mtg_setcode%22%2C%22is_foil%22%2C%22mtg_rarity%22%2C%22sf_colours%22%2C%22sf_types%22%2C%22sf_legalities%22%2C%22price.AUD.default%22%2C%22stock_qty%22%2C%22mtg_cmc%22%5D&numericFilters=%5B%22visibility_search%3D1%22%5D" } };
    

    To this:

    var load = new { Requests = new[] {new  { IndexName = "magento2_tcg_productiondefault_products", Params = $"query={query}&hitsPerPage=12&maxValuesPerFacet=8&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&ruleContexts=%5B%22magento_filters%22%2C%22%22%5D&facets=%5B%22mtg_setcode%22%2C%22is_foil%22%2C%22mtg_rarity%22%2C%22sf_colours%22%2C%22sf_types%22%2C%22sf_legalities%22%2C%22price.AUD.default%22%2C%22stock_qty%22%2C%22mtg_cmc%22%5D&numericFilters=%5B%22visibility_search%3D1%22%5D" }} };