c# HttpClient appears to be deleting instead of posting randomly - c#

I am issuing a POST request through the HttpClient class and reading the response. It works about 90% of the time, but sometimes the response string is {} and the endpoint I am hitting behaves as though it was issued a DELETE request with the same parameters as my POST request. This is my code
while (true)
{
HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>());
string fliptUrl = _fliptUrl + flag;
var response = _http.PostAsync(fliptUrl, content).Result;
if (!response.IsSuccessStatusCode)
{
continue;
}
string responseString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
if (responseString.Equals("404 page not found\n"))
{
throw new HttpRequestException("Flipt returned 404");
} else if (responseString.Equals("{}"))
{
continue;
}
Why would this be happening? Am I handling the asynchronous nature of the HttpClient in a weird way that is resulting in this behavior?
The print statement shown usually prints out {"key":"asdf","name":"asdf","description":"asdf","enabled":true,"createdAt":"2019-08-09T18:59:37.926184281Z","updatedAt":"2019-08-09T18:59:37.926184281Z"} and in the edge case I am encountering it prints {}

Related

Sending a Post Request to register a new user to a Web API, but receiving 400 : BadRequest

Having issues with my code [ Throwing an Unhandled exception: System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request) ] when trying to connect to WebApi
This is my first time working with await/async methods, but I am needing to return
string msgTask = await response.Content.ReadAsStringAsync(); return msgTask;
At first my Console.WriteLine(await response.Content.ReadAsStringAsync(); returned:
BadRequest {"error":"Password must be at least 8 characters, with at least 1 of each alpha, number and special characters"}
But then I inserted this check: response.EnsureSuccessStatusCode(); which Throws the System.Net.Http.HttpRequestException
Full [top-level styled] Code Below (I am only using Console.WriteLine() to help with debugging, final code will only have return msgTask;) :
HttpRequest.GetHttpResponse();
await WattTime.PostRequest.RegisterUser();
public class HttpRequest
{
public static HttpClient client = new();
public static void GetHttpResponse()
{
// GetRequestMethod to use later
}
}
namespace WattTime
{
class PostRequest : HttpRequest
{
public static async Task<string> RegisterUser()
{
string Url = "https://api2.watttime.org/v2/register";
Dictionary<string, string> parameters = new()
{
{"username", "TestUser" },
{"password", "Password#1" },
{"email", "testuser#yahoo.com" },
{"org", "XYZ" },
};
var jsonDictionary = JsonConvert.SerializeObject(parameters);
var content = new StringContent(jsonDictionary, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(Url, content);
response.EnsureSuccessStatusCode();
string msgTask = await response.Content.ReadAsStringAsync();
Console.WriteLine(msgTask);
return msgTask;
}
}
}
UPDATE I changed the format of data being sent to the API var data = #"{ ""username"": ""TestUser900"", ""password"": ""Summer$21"", ""email"": ""test65349#yahoo.com""}";
and added var postData = JsonConvert.SerializeObject(data); var postData2 = JsonConvert.DeserializeObject(postData);
If I use Console.WriteLine(await response.Content.ReadAsStringAsync()) I receive the Status and User Created, but if I use return response.StatusCode.ToString(); nothing returns
Looks like you need to change your test data. I tried to run your data in Online API testing tool and it returned the same error and later when I changed the json data it returned status 200 OK. Also I observed that every time you need to send unique data or its returning error.
Your issue may be the # symbol in your data. If sent in the URL it will need to be URL encoded. See this list of characters that need encoding https://www.w3schools.com/tags/ref_urlencode.asp
You can use HttpUtility.UrlEncode to do this on your jsonDictionary variable, HttpServerUtility and WebUtility classes also have this static method.
Change this response.EnsureSuccessStatusCode(); to this
if(response.IsSuccessStatusCode){
//Code here
}

C# Unable to view Errors on API Response just throws an Exception to Try/Catch

I am writing a program to check check if a Voucher number is Valid and I am finding it difficult to extract the Error Message from a REST API which I am working with.
C# is pretty new to me as normally VB.net but covering for someone at the moment.
Basically I have a HttpWebReqest and HttpWebResponse objects and using the below code I am making a successful request and getting a response just fine.
When everything goes well there are no problems, but for example if a voucher was invalid or the site was invalid I should get a response saying this, as I do in Postman, see below for example.
{
"message": "The given data was invalid.",
"errors": {
"voucher_no": [
"Sorry, that voucher number is invalid."
]
}
}
Instead I get thrown to the Try/Catch.. with the Exception
Error Message Error 422 unprocessable entity,
with no further details or object to check for the real message above?
try
{
using (HttpWebResponse response = mywebrequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
// I am unable to get to this part of the Code to process the Error because Try/Catch is executed instead ...
}
else
{
Stream dataStream1 = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream1);
responseFromServer = reader.ReadToEnd();
}
}
}
catch (WebException ex)
{
msgbox = new MsgBox_UI("Error", "Web Server Returned an Error", "There is a problem with this Voucher. It may be Expired or invalid at this time.", 1, false, 28);
msgbox.ShowDialog();
break;
}
If any one out there has any ideas as to how I can get this working it would be a great help.
This is by design, GetResponse will throw a WebException (1) when the request returns an 'unsuccessful' status code.
You can check the Status property on the WebException to get the statuscode
and the Response property for the response of the webserver.
The first thing it's better to use HttpClient class instead.
this code should work for you (if not let me know) :
private async Task<string> GetExtensionToken()
{
string url = "https://YourApi.com";
try
{
var httpclient = new HttpClient();
using (HttpResponseMessage response = httpclient.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
string result = content.ReadAsStringAsync().Result;
string Is_Not_Valid = "invalid";
if (result.Contains(Is_Not_Valid))
{
string token = "Whatever you want to extract if error page" ;
return token;
}
else
{
string token = "Whatever you want to extract if succeeded" ; return token;
}
}
}
}
catch (Exception ex)
{
return "Error from catch ";
}
}
Usage:
private async void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = await GetExtensionToken();
}
Ok so I took the advice of Peter above and decided to use HttpClient().
However I actually went with Restharp and installed the Nuget Package RestSharp into my Project. (Main reason is POSTMAN Code Snippet gave me the exact code to use.
Then it worked like a dream.
I am not doing it Async so here is what I found fixed my problem after adding
using RestSharp;
var client = new RestClient("https://api.voucherURL.uk/redeem");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Smart-Auth", "sk_xxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", "{\n \"voucher_no\":\"JY584111E3\",\n \"site_id\": 14\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

RestSharp "Error getting response stream (ReadAsync): ReceiveFailure Value cannot be null. Parameter name: src"

Hello all am trying to do a login to my xamarin api using RestSharp, the API ought to return status code 200 OK if the authentication works and status code 415 if the authentication fails(wrong password) and other codes depending on what the case scenario, but instead i get a status code 0 on all other case asides when the authentication pass(status code 200 ok), the source code below is how i implement
//payload am sending to the api
RequestPayload res = new RequestPayload();
res.appid = appid;
res.data = data;
res.method = "Login";
//convert to json object
var MySerializedObject = JsonConvert.SerializeObject(res);
string APIUrl = ""http://142.168.20.15:8021/RouteTask";
//create client
RestClient client = new RestClient(APIUrl);
//create request
RestRequest request = new RestRequest(Method.POST);
// set request headeer
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//request.AddJsonBody(MySerializedObject); --i have also tried this
request.AddParameter("application/json", MySerializedObject, ParameterType.RequestBody);
request.JsonSerializer.ContentType = "application/json; charset=utf-8";
request.AddParameter("RequestSource", "Web", "application/json", ParameterType.QueryString);
client.Timeout = 2000000;
var response = client.Execute(request); // where the issue appears
//RestResponse response = client.Execute(request); // i have tried this
//IRestResponse response = client.Execute(request); // i have tried this
if (response.IsSuccessful)
{
//use response data
}
on all scenerio it comes back with a StatusCode: 0, Content-Type: , Content-Length: 0) and errorMessage
"Error getting response stream (ReadAsync): ReceiveFailure Value
cannot be null. Parameter name: src"
screenshot below indicate when the api call fails
Response receieved when the authentication is valid
I was finally able to find a workaround for this. Bear with the long-winded response.
The tags mention Xamarin, which is what I am working in as well - specifically with iOS. I think it may actually be a bug with Mono, but I didn't take it that far to confirm.
The problem lies with the default way of copying the response buffer. In the RestSharp code, this is done by an extension method in MiscExtensions.cs called ReadAsBytes. It appears that with certain response buffers, the call to the Stream.Read method is failing. When this happens, the exception causes RestSharp to "shortcut" the rest of the processing on the response, hence the status code never gets filled in since it happens after the call to ReadAsBytes.
The good news is RestSharp does give a way to replace this call to ReadAsBytes with one of your own. This is done via the ResponseWriter property on the IRestRequest object. If it has a function defined, it will bypass the ReadAsBytes call and call the function you gave it instead. The problem is, this is defined as an Action and you don't get a copy of the full response object, so it's somewhat useless. Instead you have to use the AdvancedResponseWriter property. This one includes both the response object and the response stream. But you still have to set the ResponseWriter property or it won't bypass the default handler and you'll still get the error.
Ok, so how do you make this work? I ended up implementing it as a wrapper to RestClient so I wouldn't have to implement the code all over the place. Here's the basic setup:
public class MyRestClient : RestClient
{
public MyRestClient(string baseUrl) : base(baseUrl)
{ }
public override IRestResponse Execute(IRestRequest request)
{
request.ResponseWriter = s => { };
request.AdvancedResponseWriter = (input, response) => response.RawBytes = ReadAsBytes(input);
return base.Execute(request);
}
private static byte[] ReadAsBytes(Stream input)
{
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream())
{
int read;
try
{
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{ ms.Write(buffer, 0, read); }
return ms.ToArray();
}
catch (WebException ex)
{ return Encoding.UTF8.GetBytes(ex.Message); }
};
}
}
The ReadAsBytes method is actually just a copy/paste of the RestSharp ReadAsBytes method with the addition of a try/catch. If it fails, it returns the exception reason in to the response buffer. This may or may not be what you want, so modify as needed. You may also need to override other methods for Execute, but in my case this is the only one we're using so it was enough.
So far this seems to be doing the trick for me. Perhaps if someone got ambitious they could trace it all the way in to Mono to try and see what it doesn't like about the stream, but I don't have the time for it at the moment.
Good luck!
OK so after toying around with RestSharp for a bit, i realize just as #steve_In_Co mentioned earlier there were compatibility issues with MONO (we presume this is a bug) so i did it in a basic way using the .Net HTTP library and it works for me, so in case someone is still looking for a way out, find the working .net http implementation code below.
//payload am sending to the api
RequestPayload res = new RequestPayload();
res.appid = appid;
res.data = data;
res.method = "Login";
//convert to json object
var MySerializedObject = JsonConvert.SerializeObject(res);
string APIUrl = ""http://142.168.20.15:8021/RouteTask";
//create basic .net http client
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(APIUrl);
// this was required in the header of my request,
// you may not need this, or you may need to adjust parameter
//("RequestSource","Web") or you own custom headers
client.DefaultRequestHeaders.Add("RequestSource", "Web");
// this class is custom, you can leave it out
connectionService = new ConnectionService();
//check for internet connection on users device before making the call
if (connectionService.IsConnected)
{
//make the call to the api
HttpResponseMessage response = await
client.PostAsJsonAsync(ApiConstants.APIDefault, res);
if (response.IsSuccessStatusCode)
{
string o = response.Content.ReadAsStringAsync().Result;
dynamic payload = JsonConvert.DeserializeObject(o);
string msg = payload["valMessage"];
resp.a = true;
resp.msg = payload["responseDescription"];
}
else
{
string o = response.Content.ReadAsStringAsync().Result;
dynamic payload = JsonConvert.DeserializeObject(o);
resp.a = false;
resp.msg = payload["response"];
}
}

Post to HTTP and get JSON response back in c#

I am trying to write call a web page that then posts to a web service that outputs a JSON file.
The problem I have is that the GetAsync returns a null value for response. This in turn doesn't provide the proper URL for call back for the GetTestResultAsync method.
Here's my code:
static HttpClient client = new HttpClient();
static async Task RunAsync()
{
// New code:
client.BaseAddress = new Uri("http://10.1.10.10:8080/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
Uri url = await CallTestAsync();
string response = await GetTestResultAsync(url.PathAndQuery);
Console.WriteLine(response);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static async Task<Uri> CallTestAsync()
{
HttpResponseMessage response = await client.GetAsync("test.html");
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response.Headers.Location;
}
static async Task<string> GetTestResultAsync(string path)
{
HttpResponseMessage response = await client.GetAsync(path);
string streamResponse = string.Empty;
if (response.IsSuccessStatusCode)
{
streamResponse = await response.Content.ReadAsStringAsync();
}
return streamResponse;
}
static void Main(string[] args)
{
RunAsync().Wait();
}
HttpClient by default will automatically redirect 3xx responses, which means that the response you get when calling GetAsync will not have the Location header as it would have already redirected to the proper location.
To override this behaviour you have to provide an HttpMessageHandler with this feature disabled. For example:
static HttpClientHandler handler = new HttpClientHandler { AllowAutoRedirect = false };
static HttpClient client = new HttpClient(handler);
The important part here being setting the handler's AllowAutoRedirect to false.
Important: By overriding the default behaviour for redirect responses you'll have to handle any 3xx manually, which might add unnecessary work for you as in many cases the default behaviour is sufficient. If you were to leave it as is, it'd already make the 2nd request that you're doing, for you.
Also note that a 3xx response is not a success response, which means that if you don't use the auto-redirect feature when you call response.EnsureSuccessStatusCode(); it'll throw an exception.
Furthermore, although most servers are quite forgiving when it comes to headers such as the Accept header, you are most likely using the wrong one in this case as an HTML page should be a text/html rather than an application/json (which should be used when expecting a JSON object as a response).

CURL Response "200", GetAsync "406"

I'm attempting to get a list of orders for my Xamarin Form application from my rails api.
Here is the code in my Xamarin Application that returns a "406":
async void UpdateOrders(string token)
{
// clear all the previous orders
orders.Clear();
var client = new HttpClient();
string url = "http://localhost:3000/api/orders?access_token=" + token;
Debug.WriteLine(url);
var response = await client.GetAsync(url);
//response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
Debug.WriteLine("this didn't work");
//var ex = CreateExceptionFromResponseErrors(response);
//throw ex;
}
else
{
// get the orders from the JSON
var orderItems = JsonConvert.DeserializeObject<List<Order>>(response.ToString());
// add all the orders to the page
orders = new ObservableCollection<Order>(orderItems);
}
}
url = "http://localhost:3000/api/orders?access_token=03642494d1631421a8b49a21085b53907e8498794c0dcacc61c7b4eefbf1b7eb"
** on a side-note the CreateExceptionFromResponseErrors line throws an error?
The same url above returns the following when I run it in CURL:
[{"id":1,"invoice":"HALP","description":"TESTING","weight":100.0,"length":48,"width":48,"height":48,"account_id":1,"driver_id":null,"status":"preview","account_quote":576,"driver_quote":432,"created_at":"2017-03-11T17:18:40.418Z","updated_at":"2017-03-11T17:18:40.418Z","driver_rating":5,"account_rating":5,"guid":"cfd12c84-b260-440c-a21a-7a8aab44b5ac"}]
Where am I going wrong?
HTTP 406 is the code for "not acceptable", meaning that your HttpClient is not properly configured to receive the payload from the server. You will need to configure your header for the correct content type.
See also:
What is “406-Not Acceptable Response” in HTTP?

Categories

Resources