0

I have been dealing with this problem for a few days, currently I have a endpoint which works fine in postman but in my xamarin app it keeps returning bad request even though the resource is fine.

// Using this URL due to: https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services#android
var client = new RestClient("http://10.0.2.2:51811/api/");
var request = new RestRequest("Ruta", Method.GET);   
var response = client.Execute(request);

I was wondering if this could be related with the headers? Also breakpoints inside my server solution do not seem to be hit

Already tried:

var client = new RestClient("http://10.0.2.2:51811/api/");
var request = new RestRequest("Ruta", DataFormat.Json);          
var response = client.Get(request);
Jeremy
  • 1,447
  • 20
  • 40
  • And the IP address you are trying to hit is accessible from the network your device/emulator is on? Have you tried it in the browser on the device to see if it responds? Have you tried tool such as fiddler/charles to see what request you are sending? – Cheesebaron Jul 17 '20 at 08:41
  • Just tried accessing the server from the device's browser, I am still getting a bad request. I'm currently trying this fix: https://stackoverflow.com/questions/15724582/error-400-bad-request-when-accessing-web-api-from-xamarin-debug-device – Jeremy Jul 17 '20 at 09:27
  • Well: `new RestRequest("Ruta", DataFormat.Json);` "Ruta" is not json. You probably need to provide something else... What does the request look like in Postman? Can you maybe export and share the Curl request? – Cheesebaron Jul 17 '20 at 13:52
  • I already tried with `DataFormat.None` and of course! I can share you my Postman request collection: https://www.getpostman.com/collections/75f648e8c46a277a9202 Here's a pick of the request's headers in Postman: https://i.imgur.com/KfCncs2.png – Jeremy Jul 17 '20 at 19:43
  • have you tried https on the address? – Stefanija Jul 17 '20 at 20:50
  • The headers shouldn't really matter. Can you check the log of the API you are hitting as to why it is returning bad request? – Cheesebaron Jul 17 '20 at 21:05
  • @Stefanija I had http enabled before but it blocked me from firing any requests (https://stackoverflow.com/questions/62887536/accesing-local-server-using-resharp?noredirect=1#comment111213597_62887536), I will try configuring later on with: https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services – Jeremy Jul 17 '20 at 21:09
  • Instead of using RestSharp, can you try HttpClient directly? `var client = new HttpClient(); var stuff = await client.GetStringAsync("http://10.0.2.2:51811/api/Ruta");` – Cheesebaron Jul 17 '20 at 21:23
  • I think I may have fixed it by hosting the server using IIS instead of IIS Express: https://i.imgur.com/V2zdVkj.png, does using this have side effects on the app? – Jeremy Jul 17 '20 at 21:23
  • @Cheesebaron I already tried with the HttpClient by following: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest – Jeremy Jul 17 '20 at 21:24
  • If you can somehow see the raw request made by Postman and by RestSharp, you'd probably see the difference. – Alexey Zimarev Jul 20 '20 at 15:31

0 Answers0