LinnSystems / LinnworksNetSDK

Linnworks.net Developers
26 stars 41 forks source link

400 Bad Request when trying to update stock levels #7

Closed marcovanbeek closed 7 years ago

marcovanbeek commented 8 years ago

Hi,

I am using the php code, and trying to upload data using UpdateStockLevels and no matter what I try, I always seem to get a 400 response. I am getting the authentication token back, and then getting the warehouse id back fine. I have tried using the UpdateStockLevel class object, rolling my own array, and even building the json object "by hand" bases on the api docs, but nothing seems to work. Have you got any working examples of php code using the SDK that I could use to figure out where I am goign wrong, or even just a quick explanation of what I am supposed to send to the function?

Ta.

Marco

marcovanbeek commented 8 years ago

Bit more info. Have added a var_dump($opts) into Factory.php, and this is what I am sending (FYI: Token has been cleared): ["http"]=> array(3) { ["method"]=> string(4) "POST" ["header"]=> string(211) "Content-Type: application/x-www-form-urlencoded; charset=UTF-8 User-Agent: Linnworks PHP API SDK Referer: https://www.linnworks.net/ Content-Length: 94 Authorization: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx " ["content"]=> string(94) "stockLevels=[{"SKU":"HAL58358","LocationId":"c81c7ca2-80be-4dd9-9c55-a3e07a577f8d","Level":3}]" } } And this is what I am getting back: Warning: file_get_contents(https://ext.linnworks.net/api/Stock/UpdateStockLevelsBySKU): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /var/www/includes/linnworks/Factory.php on line 32

LinnSystems commented 7 years ago

Hi @marcovanbeek

Can you confirm that this item exists and you have the location ID correct? The 400 error should also return a body from our API, but PHP will catch this before unless you ignore CURL errors which may be something we can look into here.

I ran through this request on my side and everything seemed to be OK. I also used the information through Fiddler, changing it to my details, and couldn't seem to find any issues with this.

Would you be able to add the following to your code before making the request and then print out the response:

curl_setopt($conn, CURLOPT_FAILONERROR, false);

We will be making some changes to the PHP SDK to make it more flexible in the future, too.

marcovanbeek commented 7 years ago

Right, looks like the item has to be "added" to the warehouse before the API can add stock. This appears to be true for both the default location and any custom locations.

For reference, this SDK uses file_get_contents, not CURL, so your method above won't work, and as far as I can tell, nothing comes back from the API with the 400 error.

marcovanbeek commented 7 years ago

Is there a simple way of getting a list of all the inventory items that exist in a location, preferably with their SKU's?

LinnSystems commented 7 years ago

Hi @marcovanbeek ,

Thanks for letting me know your updates.

Apologies, our newer edition of the PHP SDK is using CURL, but this is not live yet.

If you do not have a stock level initially, you will need to set the stock level rather than update the stock level. Stock levels aren't set when a new location is created as it would drastically increase the size of the database in the instance that a user had, for example, 1 million items.

There isnt a direct call to do so, but you can use the following call to fetch an items stock levels. If a location is missing it signifies that you do not have a recorded stock level and as such will need to set it rather than update it:

http://apps.linnworks.net/Api/Method/Inventory-GetInventoryItemById

LinnSystems commented 7 years ago

This is now being closed as we have passed a reasonable amount of time for a response. Please feel free to open a new issue, or open a ticket in our ticketing system at help.linnworks.com if you have any further questions.