Search code examples
node.jsamazon-web-servicesapiamazon-mws

How can I turn this POST method into valid Node.js code?


Using https://mws.amazonservices.com/scratchpad/index.html, I am able to make a valid request to the MWS endpoint, the details of which look like this:

POST /Products/2011-10-01?AWSAccessKeyId=myAWSAccessKeyId
  &Action=GetMatchingProductForId
  &SellerId=mySellerId
  &SignatureVersion=2
  &Timestamp=2018-08-14T01%3A00%3A39Z
  &Version=2011-10-01
  &Signature=6xwEi3Mk9Ko9v9DyF9g6zA4%2Buggi7sZWTlUmNDxHTbQ%3D
  &SignatureMethod=HmacSHA256
  &MarketplaceId=ATVPDKIKX0DER
  &IdType=UPC
  &IdList.Id.1=043171884536 HTTP/1.1
Host: mws.amazonservices.com
x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
Content-Type: text/xml 

How can I take this, and turn it into a valid URL that I can use to make a request from my app, i.e., using fetch or some other javascript implementation. I tried to take the info and make a URL like this:

https://mws.amazonservices.com/Products/2011-10-01? 
AWSAccessKeyId=myAWSAccessKeyId
&Action=GetMatchingProductForId
&SellerId=mySellerId
&SignatureVersion=2
&Timestamp=2018-08-14T01%3A00%3A39Z
&Version=2011-10-01
&Signature=6xwEi3Mk9Ko9v9DyF9g6zA4%2Buggi7sZWTlUmNDxHTbQ%3D
&SignatureMethod=HmacSHA256
&MarketplaceId=ATVPDKIKX0DER
&IdType=UPC
&IdList.Id.1=043171884536 

, to which I tried to send a POST request via postman, and I got this error:

<?xml version="1.0"?>
<ErrorResponse xmlns="https://mws.amazonservices.com/">
<Error>
    <Type>Sender</Type>
    <Code>InvalidParameterValue</Code>
    <Message>Value 2
   for parameter SignatureVersion is invalid.</Message>
</Error>
<RequestID>6ded1eed-eb92-4db6-9837-3453db0f8a77</RequestID>
</ErrorResponse> 

How can I make a valid request to an MWS endpoint using javascript?


Solution

  • You could use npm module like superagent, axios or request.

    const agent = require('superagent)
    
    agent
      .post('https://mws.amazonservices.com/Products/2011-10-01')
      .query({
        AWSAccessKeyId: myAWSAccessKeyId,
        Action: GetMatchingProductForId,
        SellerId: mySellerId,
        SignatureVersion: 2,
        Timestamp: 2018-08-14T01%3A00%3A39Z,
        Version: 2011-10-01,
        Signature: 6xwEi3Mk9Ko9v9DyF9g6zA4%2Buggi7sZWTlUmNDxHTbQ%3D,
        SignatureMethod: HmacSHA256,
        MarketplaceId: ATVPDKIKX0DER,
        IdType: UPC,
        IdList.Id.1: 043171884536
      })
      .then(res => {
        console.log('here is the response');
        console.log(res)
      })
      .catch(error => {
        console.log('here is the error');
        console.log(error);
      })
    

    I haven't written against AWS but are you sure that these parameters should be sent with the querystring. Usually with post, parameters are sent with the body?

    The error you are recieving from Postman is telling you that you are reaching the server but something is wrong the with values that you are sending. For example: SignatureVersion should be 1 (or something).