繁体   English   中英

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

[英]HTTP post with JSON payload body containing an empty line using cURL

我正在通过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的此签名不匹配错误让我很头疼!

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

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

POSTing binary从文件读取时,-d将去除回车符和换行符。 如果要让curl读取并使用给定的二进制文件,请使用--data-binary,其格式与给定的完全相同:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM