ShopifySharp icon indicating copy to clipboard operation
ShopifySharp copied to clipboard

"The given header was not found" on a InventoryItemService.ListAsync call

Open shnargru opened this issue 3 years ago • 5 comments

I have an app that pulls all the Products and their Inventory using ShopifySharp. I've used this code with a half dozen different stores. I'm using now on a new store, and for some reason, the secondary InventortyItemService.ListAsync call is throwing an exception "The given header was not found". The first call works fine, it's the followup call to get the second page of inventory that this happens.

Here's the code:

	var inventoryItems = new List<InventoryItem>();
	var inventoryService = new InventoryItemService("myurl", "myapikey"));
	var inventoryIds = items.SelectMany(x => x.Variants.Select(y => y.InventoryItemId.Value)).ToList();
	var VariantInventoryResult = await inventoryService.ListAsync(new InventoryItemListFilter { Ids = inventoryIds }); //list of IDs from a previous ProductService.ListAsync call
	
	while (true)
	{
		inventoryItems.AddRange(VariantInventoryResult.Items);
		if (!VariantInventoryResult.HasNextPage) break;
		var nextPageFilter = VariantInventoryResult.GetNextPageFilter();
		VariantInventoryResult = await inventoryService.ListAsync(nextPageFilter);
	}

The error happens on that last "inventoryService.ListAsync" call. I compare this shopify store settings to another one that does work and it seems fine. Both sites are using the same Api version. I can't tell why this fails on one store but not another. What am I doing wrong?

shnargru avatar Aug 29 '22 23:08 shnargru

Thanks for the report @shnargru. Can you post the full exception plus stack trace? If possible, it'd be great to have the exception's RawBody string if it doesn't contain any private information (it's the full http response body Shopify is responding with).

nozzlegear avatar Sep 01 '22 21:09 nozzlegear

Sure, here it is:

at System.Net.Http.Headers.HttpHeaders.GetValues(HeaderDescriptor descriptor) at System.Net.Http.Headers.HttpHeaders.GetValues(String name) at ShopifySharp.ShopifyService.CheckResponseExceptions(HttpResponseMessage response, String rawResponse) at ShopifySharp.ShopifyService.<>c__DisplayClass26_01.<<ExecuteRequestAsync>b__0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.DefaultRequestExecutionPolicy.<Run>d__01.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.ShopifyService.<ExecuteRequestAsync>d__261.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.ShopifyService.<ExecuteGetCoreAsync>d__311.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.ShopifyService.<ExecuteGetListAsync>d__341.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ShopifySharp.InventoryItemService.<ListAsync>d__1.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Marketplace.Indicina.Integration.Shopify.ShopifyProvider.<GetProductsForImport>d__11.MoveNext() in C:\Prostar\Projects\Marketplace\Marketplace.Indicina\Integration\Marketplace.Indicina.Integration.Shopify\ShopifyProvider.cs:line 296 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Marketplace.Indicina.Core.Worker.ImportInventoryWorker.<Run>d__2.MoveNext() in C:\Prostar\Projects\Marketplace\Marketplace.Indicina\Core\Marketplace.Indicina.Core\Worker\ImportInventoryWorker.cs:line 33

shnargru avatar Sep 02 '22 03:09 shnargru

It unfortunately doesn't state which Header record it's looking for. This code has worked great in about 5 other stores, just this one store it's failing (and this isn't the only issue I've gotten on this store), so I think it's something to do with Shopify's API, even though the exception itself is happening in ShopifySharp's code...

shnargru avatar Sep 02 '22 03:09 shnargru

So I was able to figure it out. The InventoryItemService.ListAsync() takes a InventoryItemListFilter that can take a list if ItemIds. According to Shopify's documentation, this list is limited to 100 IDs. In this particular call, I was passing in about 500 IDs. The call to Shopify worked, I got 250 inventory items back, but then on the subsequent ListAsync() call with the GetNextPageFilter(), that is where I got the missing header error.

The fix, I had to put my list of ItemIds on a 100 'page' loop and make the call for every 100 items.

Suggestion: Either put better error handling on the ListAsync for potential missing headers, or throw an error if you supply more than 100 IDs in the InventoryItemListFilter (or both).

-shnar

shnargru avatar Sep 08 '22 15:09 shnargru

Thanks @shnargru! Glad you found the problem, that gives me something to reliably test. I don't want to throw an error if more than 100 ids are used, I'd rather let Shopify handle that particular behavior. But we definitely need a better error message or at least some kind of warning/hint when this happens. I'll see what I can do.

nozzlegear avatar Sep 08 '22 23:09 nozzlegear