amz-tools / amazon-sp-api

Amazon Selling Partner API Client
MIT License
232 stars 120 forks source link

Error: Invalid Input - for parameter values with spaces. #63

Closed andrii33 closed 2 years ago

andrii33 commented 2 years ago

Can be fixed by calling the encodeURIComponent method for parameter values. (In that case for SKUs)

API Endpoint productPricing Operation getPricing Query

{ query: { Skus: [<Some SKU With slash/44>], ItemType: 'Sku', MarketplaceId: <Some Marketplace Id> }

version: 'v0'

Error: Error: Invalid Input at SellingPartner.callAPI (/Users/Documents/projects/pricing-platform/amazon-integration-plugin/node_modules/amazon-sp-api/lib/SellingPartner.js:574:13)

amz-tools commented 2 years ago

Hi @andrii33,

the values are already being encoded (by using qs module's stringify method). Just tried it with a SKU that includes a / and it works fine. Maybe there's another issue with your request params?

andrii33 commented 2 years ago

Hi @amz-tools ,

I've tested a few times. Right now / is not supported in the SKUs values. The requests work fine with SKUs without /. And the problem appeared for SKU with a slash. Once I've added encodeURIComponent for the SKUs everything started to work without errors. I've imported a catalog with many SKUs that have slashes. And I've got errors without encodeURIComponent call for the SKUs.

amz-tools commented 2 years ago

That is really weird. I have tested the following code:

let price_for_sku = await sellingPartner.callAPI({
  operation:'productPricing.getPricing',
  query:{
    Skus:['<MY_SKU_/_WITH_SLASH>'],
    ItemType:'Sku',
    MarketplaceId:'A1PA6795UKMFR9'
  }
});

It returns the following successful response:

[
  {
    status: 'Success',
    SellerSKU: '<SKU_/_WITH_SLASH>',
    Product: {
      Identifiers: {
        MarketplaceASIN: { MarketplaceId: 'A1PA6795UKMFR9', ASIN: '<ASIN_OF_THE_SKU>' },
        SKUIdentifier: {
          MarketplaceId: 'A1PA6795UKMFR9',
          SellerId: '<SELLER_ID>',
          SellerSKU: '<SKU_/_WITH_SLASH>'
        }
      },
      Offers: [
        {
          BuyingPrice: {
            ListingPrice: { CurrencyCode: 'EUR', Amount: 6.95 },
            LandedPrice: { CurrencyCode: 'EUR', Amount: 9.95 },
            Shipping: { CurrencyCode: 'EUR', Amount: 3 }
          },
          RegularPrice: { CurrencyCode: 'EUR', Amount: 6.95 },
          FulfillmentChannel: 'MERCHANT',
          ItemCondition: 'New',
          ItemSubCondition: 'New',
          SellerSKU:  '<SKU_/_WITH_SLASH>'
        }
      ]
    }
  }
]

When I log the value of the above query (Line 36 in Signer.js) I can see the value being encoded correct:

ItemType=Sku&MarketplaceId=A1PA6795UKMFR9&Skus=SKU_%2F_WITH_SLASH

The docs of qs.stringify are actually also pretty clear on that:

When stringifying, qs by default URI encodes output.

Furthermore, we do use the getPricing operation for customers of ours as well (also includes SKUs with /) without any problems.

I think the question is: What's different with your request compared to my example request above?

andrii33 commented 2 years ago

@amz-tools Sorry, the problem is related to space, not the slash. I've tested with SKU that has space only. Example:

{"operation":"getPricing","endpoint":"productPricing","query":{"Skus":["BOSCH KEYRING"],"ItemType":"Sku","MarketplaceId":"<some marketplace id>"},"options":{"version":"v0"},"method":"GET","api_path":"/products/pricing/v0/price","restore_rate":0.1}
{"errors":[{"code":"InvalidInput","message":"Invalid Input","details":""}]}
amz-tools commented 2 years ago

@andrii33,

for spaces the problem is known already. It actually doesn't seem to be a problem with the client. When spaces are not encoded we get an InvalidSignature error, when spaces are encoded as %20 we receive the InvalidInput error you mentioned.

The issue has been discussed here https://github.com/amzn/selling-partner-api-models/issues/1351 and here https://github.com/amzn/selling-partner-api-models/issues/1857.

I think we will have to wait for a fix on Amazon's end, but we should add it to the Known Issues section.

Meanwhile you could use the operation with ASINs instead of SKUs as a quick fix.

amz-tools commented 2 years ago

@andrii33,

Update on this one: Instead of URI encoding the whitespace as %20 it works fine if we replace whitespace by + sign.

So its possible to fix the issue, we only have to check if we can fix this for all query params of all operations or if we have to take the query param into account (which would complicate it).

andrii33 commented 2 years ago

@amz-tools Thank you! I was using the encodeURIComponent to encode the parameters without encoding the full URL. So that way that works fine.