代码之家  ›  专栏  ›  技术社区  ›  adam tropp

如何将此POST方法转换为有效的Node.js代码?

  •  0
  • adam tropp  · 技术社区  · 6 年前

    使用 https://mws.amazonservices.com/scratchpad/index.html ,我能够向MWS端点发出有效请求,其详细信息如下所示:

    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 
    

    我怎样才能把它转换成一个有效的URL,可以用来从我的应用程序发出请求,也就是说,使用fetch或其他一些javascript实现。我试着获取信息并创建一个这样的URL:

    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 
    

    ,我试图通过邮递员向其发送邮件请求,但出现以下错误:

    <?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> 
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Bergur    6 年前

    可以使用NPM模块,如超级代理、AXIOS或请求。

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

    我没有写反对AWS的文章,但是您确定这些参数应该与querystring一起发送。通常使用post,参数是与body一起发送的?

    您从邮递员那里收到的错误是告诉您您正在到达服务器,但是您发送的值有问题。例如:signatureReversion应该是1(或其他值)。