带有JSON有效负载主体的HTTP帖子,使用cURL包含空行

时间:2018-10-05 01:06:25

标签: c# json curl http-post

我正在通过cURL将json净荷发送到服务器后端(.NET .ashx(c#)处理程序),如下所示:

curl -X POST -d@filename.txt http://localhost:49797/Handler.ashx -H "Content-Type:application/json"

JSON(位于filename.txt中)中有一个空行(如果您认识到它来自https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html,我正在使用它来验证我的AWS4签名代码)

{ "expiration": "2015-12-30T12:00:00.000Z",
  "conditions": [
   {"bucket": "sigv4examplebucket"},
    ["starts-with", "$key", "user/user1/"],
    {"acl": "public-read"},
    {"success_action_redirect":"http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"},
    ["starts-with", "$Content-Type", "image/"],
    {"x-amz-meta-uuid": "14365123651274"},
    {"x-amz-server-side-encryption": "AES256"},
    ["starts-with", "$x-amz-meta-tag", ""],

    {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20151229T000000Z" }
  ]
}

我正在读取服务器上的有效负载,如下所示:

private string getPayloadString(HttpRequest Request)
    {
        string documentContents;
        using (Stream receiveStream = Request.InputStream)
        {
            using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
            {
                documentContents = readStream.ReadToEnd();
            }
        }
        return documentContents;
    }

当我在Visual Studio中检查'documentContents'时,基本上得到如下字符串:

{ "expiration": "2015-12-30T12:00:00.000Z",  "conditions": [    {"bucket": "sigv4examplebucket"},    ["starts-with", "$key", "user/user1/"],    {"acl": "public-read"},    {"success_action_redirect": "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"},    ["starts-with", "$Content-Type", "image/"],    {"x-amz-meta-uuid": "14365123651274"},    {"x-amz-server-side-encryption": "AES256"},    ["starts-with", "$x-amz-meta-tag", ""],    {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request"},    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},    {"x-amz-date": "20151229T000000Z" }  ]}

当我对base64Encode进行编码时,它与AWS页面上的期望值不匹配(如果在编码之前我确实在服务器代码中对字符串进行了硬编码,则它确实与之匹配)

我的问题是

  1. 我在读取有效载荷时失去了空白吗?如果是这样,我该怎么办?
  2. cURL是否不完全发送txt文件中的数据?
  3. 当我在文本可视化器中查看它时,是Visual Studio剥离出来吗?

谢谢,这个签名与AWS的错误不匹配,让我很头疼!

1 个答案:

答案 0 :(得分:0)

我建议使用带有卷曲的--data-binary来保持换行。

从这里https://ec.haxx.se/http-post.html

  

POSTing binary从文件中读取时,-d将去除回车   返回和换行符。如果您想读取curl并使用--data-binary   完全按照给定的二进制格式使用给定的文件

     

curl --data-binary @文件名http://example.com/