locked
serialize json string to a form post data RRS feed

  • Question

  • User-1104215994 posted

    Hi guys,

    I have a web API and I am calling another API inside of a controller as follows. I wonder if I can convert <g class="gr_ gr_146 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="146" data-gr-id="146">json</g> string content to form post data type while sending to this API.

    // POST: api/Game
            //[RequireHttps] For Prod Only
            [HttpPost, Route("initiation")]
            public async Task<IHttpActionResult> PostInitiate(InitiateRequest initiate)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
    
             
                #region Call Game
                var httpClient = new HttpClient();
    
                HttpContent content = new StringContent(
                    JsonConvert.SerializeObject(initiate),
                    Encoding.UTF8,
                    "application/x-www-form-urlencoded"
                );
    
    
                var response =
                     await httpClient.PostAsync("https://test.com/purchaseinitiation",content);
    
              
                switch (response.StatusCode)
                {
                    case HttpStatusCode.NotFound:
                    {
                        return NotFound();
                    }
    
                    case HttpStatusCode.InternalServerError:
                    {
                        return InternalServerError();
                    }
    
                    case HttpStatusCode.OK:
                    {
                        
                        //Adding Response into database
                        context.InitiateResponses.Add(initiateResponse);
                        await context.SaveChangesAsync();
                        return Ok(initiateResponse);
                    }
    
                    case HttpStatusCode.BadRequest:
                    {
                        return BadRequest(htmlResponse);
                    }
    
                    case HttpStatusCode.Unauthorized:
                    {
                        return Unauthorized();
                    }
    
                    case HttpStatusCode.RequestTimeout:
                    {
                        return InternalServerError();
                    }
    
                    default:
                    {
                        htmlResponse = await response.Content.ReadAsStringAsync();
                        break;
                    }
                }
    
                return Ok(htmlResponse);
    
            }

    My sample <g class="gr_ gr_258 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="258" data-gr-id="258">http</g> content:

    {"ApplicationCode"="52e7cf966b724749a7c4efadc3727ed7","productCode":"000000006479","quantity":1,"version":"V1","signature":"7a98eaa454750b9b591ee7a65f13accb","customerID":1}

    How I would like to send:

    ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1
    

    Saturday, April 6, 2019 7:11 PM

Answers

All replies

  • User475983607 posted

    Your question and code is confusing.   For starters, the code does not compile.  Content is not defined.  Is there any reason why you can't simply deserialize the JSON into a an object the use basic string concatenation to build the POST data?

    Saturday, April 6, 2019 8:22 PM
  • User-1104215994 posted

    If I deserialize it into an object how can I post as follows?

    var response =
                     await httpClient.PostAsync("https://test.com/purchaseinitiation",content);

    Saturday, April 6, 2019 8:39 PM
  • User-2054057000 posted

    cenk1536

    If I deserialize it into an object how can I post as follows?

    var response =
                     await httpClient.PostAsync("https://test.com/purchaseinitiation",content);

    You have to do something like this:

    Reference -  Consuming an API in ASP.NET Core

    StringContent content = new StringContent(JsonConvert.SerializeObject(yourObject), Encoding.UTF8, "application/json");
     
    var response = await httpClient.PostAsync("https://test.com/purchaseinitiation", content));
    
    Sunday, April 7, 2019 5:58 AM
  • User-1104215994 posted

    did you check my code <g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="14" data-gr-id="14">yogyogi</g>?

    Sunday, April 7, 2019 6:23 AM
  • User-2054057000 posted

    cenk1536

    did you check my code yogyogi?

    Instead of JSON you would like to send the data as:

    ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1

    You can do it the same way:

    StringContent content = new StringContent("ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1"); 
    var response = await httpClient.PostAsync("https://test.com/purchaseinitiation", content));
    Sunday, April 7, 2019 9:52 AM
  • User-1104215994 posted

    Is there an easy way to build string as follows?

    "ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1
    Sunday, April 7, 2019 9:55 AM
  • User475983607 posted

    Is there an easy way to build string as follows?

    "ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1

    See the following SO post.

    https://stackoverflow.com/questions/47466758/c-sharp-build-url-encoded-query-from-model-object-for-httpclient

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 7, 2019 11:25 AM
  • User-1104215994 posted

    thank you.

    Sunday, April 7, 2019 1:23 PM
  • User-2054057000 posted

    Is there an easy way to build string as follows?

    "ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1

    I would prefer StringBuilder class here.

    Sunday, April 7, 2019 2:54 PM