Guillaume Raymond
Guillaume Raymond

Reputation: 1824

Why RestSharp get 400 - BadRequest while Fiddler get 200 - OK?

I'm surely missing a point but have you got any idea why my client call using RestSharp (v 104.4.0) get 400 - Bad Request when in the same time if I request using fiddler I get 200 - OK

Below is my client code:

  public System.IO.Stream OpenRead(string assetId)
    {
        var request = PresentationServer.CreateRequest("/" + CurrentPlatform.Id + "/assets/" + assetId, true, Method.GET);
        var response = RestClient.Execute<SingleResource<Asset>>(request);
        if (response.Data == null)
            return null;

        var asset = response.Data.Resource;

        if (response.Data.Resource.MimeType.Contains("image"))
        {
            var contentUri = response.Data.Links.FirstOrDefault(el => el.Rel == "content");
            if (contentUri == null) throw new Exception("Asset content Uri not found");

            var contentRequest = PresentationServer.CreateRequest(contentUri.Href, true, Method.GET);
            contentRequest.AddHeader("Accept",asset.MimeType);
            contentRequest.AddHeader("Content-Type", asset.MimeType);

            var contentResponse = RestClient.Execute(contentRequest);

            if (contentResponse.StatusCode != HttpStatusCode.OK)
                throw new Exception("Asset content request failed :" + contentResponse.ErrorMessage);

            var stream = new MemoryStream();
            stream.Write(contentResponse.RawBytes, 0, contentResponse.RawBytes.Length);
            return stream;

        }

        return null;
    }

RestSharp's ContentResponse.Content get this kind of error:

System.Web.HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (:). at System.Web.HttpRequest.ValidateInputIfRequiredByConfig() at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context)

If a use a similar request from fiddler composer I get successful response:

enter image description here

Upvotes: 1

Views: 1827

Answers (3)

darkProgrammer
darkProgrammer

Reputation: 1

HttpWebRequest.DefaultMaximumErrorResponseLength default value is 65536.

You should consider increasing it.

Upvotes: -1

Guillaume Raymond
Guillaume Raymond

Reputation: 1824

It was a mistake from our side, and the error message was explicit:

A potentially dangerous Request.Path value was detected from the client (:)

We did a bad request because we add plain url to root server url:

http://RESTserver/api/http://RESTserver/api/resources/xxxxx/content

Thanks to all who take some time trying to help me.

Upvotes: 1

reptildarat
reptildarat

Reputation: 1036

There are great articles that addressing this problem see here: Article

You can change this behavior in your config file:

<system.web>
    <httpRuntime requestPathInvalidCharacters="<,>,*,%,&,:,\,?" />
</system.web>

Or get back to .Net 2.0 validation:

<system.web>
    <httpRuntime requestValidationMode="2.0" />
</system.web>

Upvotes: 1

Related Questions