Can RestSharp send binary data without using a multipart content type?

后端 未结 6 2221
闹比i
闹比i 2020-12-14 12:48

I have been using AddParameter to include XML bodies in my HTTP requests:

request.AddParameter(contentType, body, ParameterType.RequestBody);
         


        
6条回答
  •  独厮守ぢ
    2020-12-14 13:03

    I had the same issue. Turned out that RestSharp behaves in a little odd way.

    NOT WORKING:

    request.Parameters.Add(new Parameter() {
      ContentType = "application/x-www-form-urlencoded",
      Type = ParameterType.RequestBody,
      Value = bytes
    });
    

    WORKING (Add content-type as name):

    request.Parameters.Add(new Parameter() {
      Name = "application/x-www-form-urlencoded", // This is the 'funny' part
      ContentType = "application/x-www-form-urlencoded",
      Type = ParameterType.RequestBody,
      Value = bytes
    });
    

    I tried this solution based on a comment here: https://github.com/restsharp/RestSharp/issues/901

    which states "...name value will be used as Content-Type Header and contentType value will be ignored."

    You dont have to add the value as the Content-Type parameter as well, but I fear that a future bug-fix might change the behaviour and then requiring the Content-Type to be used instead of name.

提交回复
热议问题