当JavaScript版本为201时,c#httpclient返回400错误

时间:2020-05-11 00:08:29

标签: c# header httpclient

我有一个工作的脚本示例,用于发送post请求。

  var payload = { market: req.body.market, order: { price: req.body.order.price, side: req.body.order.side, size: req.body.order.size } };
  var headers = {
        'cx-api-key': apiKey,
        'cx-content-hash': contentHash,
        'cx-timestamp': timestamp,
        'cx-signature': signature
    };

  axios.post('https://test.com/orders', payload, { headers: headers })
        .then(function (response) {
            if (response.status === 201) {
                res.sendStatus(201);                    
            }
        })
        .catch(function (error) {
            res.status(500).send({ error: error });
        });

现在将其转换为c#,我做了

 var payload = JsonConvert.SerializeObject(vm);
 var client = new HttpClient();
 var httpRequestMessage = new HttpRequestMessage
 {
      Method = HttpMethod.Post,
      RequestUri = new Uri("https://test.com/orders"),
      Headers = {          
         { "cx-api-key", apiKey },
         { "cx-content-hash", contentHash },
         { "cx-timestamp", timestamp.ToString() },
         { "cx-signature", signature }
       },
       Content = new StringContent(payload)
 };
 var response = await client.SendAsync(httpRequestMessage);

我检查了apikeycontentHashtimestampsignaturepayload的值是否与javascript版本匹配。 知道为什么我要400吗?我假设是因为标头中的timestamp被转换为字符串值。但是我知道应该将其作为文本值通过http传输。

0 个答案:

没有答案