/// <summary> /// Prepares the client to send a PUT request containing json /// </summary> public PreparedRequest PreparePutJson(object data, CancellationToken cancellationToken = default(CancellationToken)) { Verb = HttpMethod.Put; Content = new CapturedJsonContent(data); CancellationToken = cancellationToken; return(this); }
/// <summary> /// Prepares the client to send a POST request containing json /// </summary> public PreparedRequest PreparePostJson(object data, CancellationToken cancellationToken = default(CancellationToken)) { Verb = HttpMethod.Post; Content = new CapturedJsonContent(Settings.JsonSerializer.Serialize(data)); CancellationToken = cancellationToken; return(this); }
/// <summary>Sends an asynchronous POST request.</summary> /// <param name="request">The IFlurlRequest instance.</param> /// <param name="data">Data to parse.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param> /// <returns>A Task whose result is the response body as a Stream.</returns> public static Task <Stream> PostJsonStreamAsync( this IFlurlRequest request, object data, CancellationToken cancellationToken = default, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) { using var capturedJsonContent = new CapturedJsonContent(request.Settings.JsonSerializer.Serialize(data)); return(request.SendAsync(HttpMethod.Post, (HttpContent)capturedJsonContent, cancellationToken, completionOption).ReceiveStream()); }
/// <summary> /// request url that specified by yourself with post method. /// according the package Flurl.Http.GeneratedExtensions to rewrite it in order to debug, develop. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="data"></param> /// <param name="cancellationToken"></param> /// <param name="completionOption"></param> /// <returns></returns> public static Task <T> PostJsonAsync <T>(this Flurl.Url url, object data, CancellationToken cancellationToken = default(CancellationToken), HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead) { var request = new FlurlRequest(url); CapturedJsonContent content = new CapturedJsonContent(request.Settings.JsonSerializer.Serialize(data)); var response = request.SendAsync(HttpMethod.Post, content, cancellationToken, completionOption); var result = response.ReceiveJson <T>(); return(result); }
public static async Task <DownloadRequest> DownloadAsync <T>(this string url, IActionContext context, object queryString = null, object model = null, bool isMethodGet = true) { try { if (queryString != null && !url.EndsWith("?")) { url += "?"; //append querystring delmiter } DownloadRequest download = new DownloadRequest(); var request = ToQueryStringInternal(url, queryString).WithStandardHeaders(context); var httpMethod = HttpMethod.Get; if (!isMethodGet) { httpMethod = HttpMethod.Post; } CapturedJsonContent capturedJsonContent = new CapturedJsonContent(request.Settings.JsonSerializer.Serialize(model)); HttpResponseMessage httpResponseMessage = await request.SendAsync(httpMethod, isMethodGet == true?null : capturedJsonContent, new CancellationToken?(), HttpCompletionOption.ResponseHeadersRead); HttpContent content = httpResponseMessage.Content; if (content.Headers.ContentDisposition != null) { download.FileName = content.Headers.ContentDisposition.FileName.StripQuotes(); download.ContentType = content.Headers.ContentType.MediaType; download.DispositionType = content.Headers.ContentDisposition.DispositionType; } download.PushStreamFunction = async(stream) => { using (var downloadStream = await httpResponseMessage.Content.ReadAsStreamAsync()) using (var reader = new StreamContent(downloadStream)) using (stream) //this has to be called in order to signal caller that we have finished { await reader.CopyToAsync(stream); } }; return(download); } catch (FlurlHttpException ex) { if (ex.Call?.HttpStatus == HttpStatusCode.BadRequest) { var validationResult = ex.GetResponseJson <ValidationResult>(); if (validationResult != null) { throw new ValidationException(validationResult); } } throw; } }
public async Task <TModel> PutAsync <TModel>(string path, string query) where TModel : new() { return(await _circuitBreaker.ExecuteAsync(() => { var jsonContent = new CapturedJsonContent(query); return _client.Request(path) .PutAsync(jsonContent) .ReceiveJson <TModel>(); })); }
/// <summary> /// Adds an HttpResponseMessage to the response queue with the given data serialized to JSON as the content body. /// </summary> /// <param name="body">The object to be JSON-serialized and used as the simulated response body.</param> /// <param name="status">The simulated HTTP status. Default is 200.</param> /// <param name="headers">The simulated response headers (optional).</param> /// <param name="cookies">The simulated response cookies (optional).</param> /// <returns>The current HttpTest object (so more responses can be chained).</returns> public HttpTest RespondWithJson(object body, int status = 200, object headers = null, object cookies = null) { var content = new CapturedJsonContent(FlurlHttp.GlobalSettings.JsonSerializer.Serialize(body)); return(RespondWith(content, status, headers, cookies)); }
/// <summary> /// Adds an HttpResponseMessage to the response queue with the given data serialized to JSON as the content body. /// </summary> /// <param name="body">The object to be JSON-serialized and used as the simulated response body.</param> /// <param name="status">The simulated HTTP status. Default is 200.</param> /// <param name="headers">The simulated response headers (optional).</param> /// <param name="cookies">The simulated response cookies (optional).</param> /// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names of headers will be replaced by hyphens. Default is true.</param> /// <returns>The current HttpTest object (so more responses can be chained).</returns> public HttpTest RespondWithJson(object body, int status = 200, object headers = null, object cookies = null, bool replaceUnderscoreWithHyphen = true) { var content = new CapturedJsonContent(Settings.JsonSerializer.Serialize(body)); return(RespondWith(content, status, headers, cookies, replaceUnderscoreWithHyphen)); }