How do I get raw request body using servicestack with content-type set to application/x-www-form-urlencoded?

shawhu

I have my DTO

Route("/testing","POST")]
public class TestingRequest:IRequiresRequestStream
{
    public Stream RequestStream { get; set; }
}

and my service

public async Task<object> Post(TestingRequest req)
{
    var rawbody = base.Request.GetRawBody();
    var data = req.RequestStream.ReadFully();
    var strdata = UTF8Encoding.UTF8.GetString(data);
    ...
}

And I found that when calling it, rawbody or data is not empty only if content-type is not application/x-www-form-urlencoded. If the content type is application/x-www-form-urlencoded, rawbody and data will be empty.

How do I get the raw request body as a whole (string) when the caller set the content-type to be application/x-www-form-urlencoded?

My current env is ubuntu 16.04 with dotnet core 1.1, don't know if it matters

mythz

ServiceStack uses a forward only request stream that unless you tell ServiceStack to buffer the Request Stream with a pre-request filter:

PreRequestFilters.Add((httpReq, httpRes) => {
    httpReq.UseBufferedStream = true;
});

Either ServiceStack reads or you can read by having your Request DTOs implement IRequiresRequestStream to tell ServiceStack to skip reading the Request Body and that you're going to in your Services.

application/x-www-form-urlencoded requests are special in that if IRequest.FormData is accessed it will trigger reading the Request Body. You can tell ServiceStack to skip reading the FormData when it creates the Request with:

SetConfig(new HostConfig {
    SkipFormDataInCreatingRequest = true
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How is Laravel decoding HTTP request body Content-Type: application/x-www-form-urlencoded when using api call

How do I post data using okhttp library with content type x-www-form-urlencoded?

How to perform a GET request with application/x-www-form-urlencoded content-type in Go?

How do I get the raw request body from the Request.Content object using .net 4 api endpoint

Why does Axios send my POST request with Content-Type application/x-www-form-urlencoded when using a string as the request body?

How to send post request with x-www-form-urlencoded body

How to send post request with content-type x-www-form-urlencoded android retrofit

Swift - How to send POST request with "x-www-form-urlencoded" content-type

How test Post request with custom object in content type application/x-www-form-urlencoded?

How to send a POST request with Content-Type "application/x-www-form-urlencoded"

How do I set up a function app to extract data from media type 'application/x-www-form-urlencoded'

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

how to post body x-www-form-urlencoded using webclient?

How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

RestAssured : annot determine how to serialize content-type application/x-www-form-urlencoded. How to create request with key/value structure?

How can I set x-www-form-urlencoded in SoapUI?

How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8?

Http Put request with content type application/x-www-form-urlencoded not working in Spring

Order of request parameters for content-type application/x-www-form-urlencoded in Spring MVC

How do I get HTTP Request body content in Laravel?

RestSharp post request - Body with x-www-form-urlencoded values

How to send post request with x-www-form-urlencoded body with loopj-async-http library

Karate DSL: How to add specific key and value (XML) as x-www-form-urlencoded to body of request?

PYTHON: requests.post() how to send request_body encoded as application/x-www-form-urlencoded

Content-Type = 'application/x-www-form-urlencoded' in request is changed to Content-Type: application/json; in Karate version 0.9.2

file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded

Trying to reach API using Content-Type application/x-www-form-urlencoded

my request failed when the post 'content-type' is application/x-www-form-urlencoded and * form field param= { <this is a json object>}

TOP Ranking

HotTag

Archive