1 year ago

#248270

test-img

Steve

How to issue Post request using Flurl with Content-Type and request body

I have this endpoint

   [HttpPost]
   public async Task<IHttpActionResult> PutHello(int id, string code) {
       string root = Path.GetTempPath();
       var provider = new MultipartFormDataStreamProvider(root);

       // Read the form data.
       await Request.Content.ReadAsMultipartAsync(provider);

       // code removed for brevity
    }

How do I construct my post request using Flurl?

At the moment, I have this code

string endPoint = "/api/abc/";

string url = string.Format("{0}{1}{2}", baseUrl, endPoint, clientId);
var response = await url.SetQueryParams(new { id = 1, code = 2 })
.PostJsonAsync(postData);

When I debug, the request does reach the endpoint. The 2 querystring are correct. But it could not get the data from postData

Below is the valid Ruby code for your reference. I need to migrate from Ruby to C#.

  def self.post_test(id, code, raw_request, raw_request_body)
    uri = URI.parse("#{protocol}://#{ip}/api/abc/#{id}?#{code}")

    request = Net::HTTP::Post.new(uri.to_s)
    request["Content-Type"] = raw_request.env["CONTENT_TYPE"]
    request.body = raw_request_body

    response = Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
      http.request(request)
    end

    result = response.body
    json = JSON.parse(result)

    return json
  end

c#

flurl

0 Answers

Your Answer

Accepted video resources