I have a web API and I am calling another API inside of a controller as follows. I wonder if I can convert json 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);
}
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?
StringContent content = new StringContent(JsonConvert.SerializeObject(yourObject), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://test.com/purchaseinitiation", content));
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
StringContent content =newStringContent("ApplicationCode=52e7cf966b724749a7c4efadc3727ed7&productCode=000000006479&quantity=1&version=V1&signature=935d356168de293eecb994316ce6cbf9&customerID=1"); var response = await httpClient.PostAsync("https://test.com/purchaseinitiation", content));
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
527 Points
2729 Posts
serialize json string to a form post data
Apr 06, 2019 07:11 PM|cenk1536|LINK
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 json string content to form post data type while sending to this API.
My sample http content:
How I would like to send:
All-Star
53051 Points
23634 Posts
Re: serialize json string to a form post data
Apr 06, 2019 08:22 PM|mgebhard|LINK
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?
Member
527 Points
2729 Posts
Re: serialize json string to a form post data
Apr 06, 2019 08:39 PM|cenk1536|LINK
If I deserialize it into an object how can I post as follows?
Participant
1253 Points
936 Posts
Re: serialize json string to a form post data
Apr 07, 2019 05:58 AM|yogyogi|LINK
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));
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
527 Points
2729 Posts
Re: serialize json string to a form post data
Apr 07, 2019 06:23 AM|cenk1536|LINK
did you check my code yogyogi?
Participant
1253 Points
936 Posts
Re: serialize json string to a form post data
Apr 07, 2019 09:52 AM|yogyogi|LINK
Instead of JSON you would like to send the data as:
You can do it the same way:
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
527 Points
2729 Posts
Re: serialize json string to a form post data
Apr 07, 2019 09:55 AM|cenk1536|LINK
Is there an easy way to build string as follows?
All-Star
53051 Points
23634 Posts
Re: serialize json string to a form post data
Apr 07, 2019 11:25 AM|mgebhard|LINK
See the following SO post.
https://stackoverflow.com/questions/47466758/c-sharp-build-url-encoded-query-from-model-object-for-httpclient
Member
527 Points
2729 Posts
Re: serialize json string to a form post data
Apr 07, 2019 01:23 PM|cenk1536|LINK
thank you.
Participant
1253 Points
936 Posts
Re: serialize json string to a form post data
Apr 07, 2019 02:54 PM|yogyogi|LINK
I would prefer StringBuilder class here.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠