Xamarin.Auth - how to directly set the request body of a POST?

It looks like Xamarin.Auth provides for either form-encoded requests via the parameters dictionary, or multi-part content via AddMultipartData. I don't see a way for me to POST a simple JSON-encoded request body, which seems crazy.

Am I missing something?

Tagged:

Posts

  • TonyAndrewsTonyAndrews USMember

    I found a workaround. Subclass OAuth2Request and add a method like this:

            public async Task SetBodyAsync(string content)
            {
                var request = this.GetPreparedWebRequest();
    
                var rawContent = UTF8Encoding.Default.GetBytes(content);
                request.ContentLength = rawContent.Length;
                request.ContentType = "application/json";
    
                var stm = await request.GetRequestStreamAsync();
                await stm.WriteAsync(rawContent, 0, rawContent.Length);
            }
    
  • RubitoRubito USMember

    Thanks, works perfectly for me, btw you can actually make the function shorter:

        public virtual void SetRequestBody(string body)
        {
            var request = this.GetPreparedWebRequest();
            this.request.Content = new StringContent(body, Encoding.UTF8, "application/json");
        }
    
Sign In or Register to comment.