Palemera
Palemera

Reputation: 17

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.

Upvotes: 1

Views: 1347

Answers (3)

sa-es-ir
sa-es-ir

Reputation: 5082

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" }} };

Upvotes: 1

derpirscher
derpirscher

Reputation: 17407

In your postman request, the requests property of your post-body is an array

{
  "requests": [{...}]
}

Whereas in your C# code, it's an object

var load = new { 
  Requests = new { ... } 
}

So the server probably cannot get the required data out of your request. Create the requests property as a List<object> or object[], so it will be serialized to an array

var load = new {
  Requests = new List<object>{new {...}}
}

Upvotes: 1

Seabizkit
Seabizkit

Reputation: 2415

see comment in code below also include the error..

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" 
        } 
    };

    //insepct this..... im sure this is probably not what you think it is.
    //worth double checking
    var resultS = GetStringContent(load); //inspect the result value.

    var message = await _client.PostAsync(apiUrl, resultS);
    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");
}

Upvotes: 0

Related Questions