3
votes

I am able to do a GET with similar code and the Bearer Token, but cannot seem to do the POST.

When I copy/paste the Json, the URL and the Bearer Token into Postman it works perfectly. But when doing it from the C# I get this error:

"StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Transfer-Encoding: chunked Connection: keep-alive Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" CF-RAY: 4c190ad8ad786539-SYD Date: Wed, 03 Apr 2019 06:38:54 GMT Set-Cookie: __cfduid=dc0232e99fa0fefc0bd728258229dd5d51554273534; expires=Thu, 02-Apr-20 06:38:54 GMT; path=/; domain=.paymentsapi.io; HttpOnly; Secure Server: cloudflare X-Powered-By: ASP.NET Content-Type: application/json; charset=utf-8 }"

Can anyone see what I'm doing wrong?

Thanks in advance.

JsonSerializerSettings jss = new JsonSerializerSettings();
string strValue = JsonConvert.SerializeObject(TestMaster, jss);
lblJSon.Text = strValue;        // This Json is valid
ByteArrayContent bytecontent = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes(strValue));
bytecontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

string AccessToken = lblToken.Text;

HttpClient tRequest = new HttpClient();
tRequest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Task<HttpResponseMessage> getTask = tRequest.PostAsJsonAsync(new Uri(strURL).ToString(), bytecontent);

HttpResponseMessage urlContents = await getTask;

Console.WriteLine("urlContents.ToString");
lblEDDR.Text = urlContents.ToString();
1
Probably because of "System.Text.Encoding.UTF8.GetBytes", try using System.Convert.FromBase64String - Code Name Jack

1 Answers

6
votes

PostAsJsonAsync converts your ByteArrayContent into a json object. You can use ether PostAsJsonAsync directly with your TestMaster like so:

string AccessToken = lblToken.Text;

HttpClient tRequest = new HttpClient();
tRequest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Task<HttpResponseMessage> getTask = tRequest.PostAsJsonAsync(new Uri(strURL).ToString(), TestMaster);

HttpResponseMessage urlContents = await getTask;

Console.WriteLine("urlContents.ToString");
lblEDDR.Text = urlContents.ToString();

Or you convert TestMaster to Json and use PostAsync with a StringContent object. Like so:

JsonSerializerSettings jss = new JsonSerializerSettings();
string strValue = JsonConvert.SerializeObject(TestMaster, jss);
lblJSon.Text = strValue;        // This Json is valid
StringContent strcontent = new StringContent (strValue);
bytecontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

string AccessToken = lblToken.Text;

HttpClient tRequest = new HttpClient();
tRequest.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

Task<HttpResponseMessage> getTask = tRequest.PostAsync(new Uri(strURL).ToString(), bytecontent);

HttpResponseMessage urlContents = await getTask;

Console.WriteLine("urlContents.ToString");
lblEDDR.Text = urlContents.ToString();