我有一个工作的脚本示例,用于发送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);
我检查了apikey
,contentHash
,timestamp
,signature
和payload
的值是否与javascript版本匹配。
知道为什么我要400吗?我假设是因为标头中的timestamp
被转换为字符串值。但是我知道应该将其作为文本值通过http传输。