0

I am developing an API in PHP where I return a json file, I perform the tests in postman and it works perfectly with the url

http://localhost:80/bdevApi/api/index/keyacessoUbasd42123/CategoriaExame

returning me

{
    "code": "200",
    "result": true,
    "message": "",
    "data": {
        "item": [
            {
                "id": "5",
                "descricao": "TesteDesc",
                "observacao": "TesteObs",
                "status": "1"
            },
            {
                "id": "7",
                "descricao": "TesteDesc",
                "observacao": "TesteObs",
                "status": "1"
            },
        ],
        "count": 15
    }
}

now I want to receive this information in a C # application using WPF. I've imported the client

public partial class MainWindow : Window
    {
        HttpClient client = new HttpClient();

        public MainWindow()
        {
            InitializeComponent();

            client.BaseAddress = new Uri("http://localhost:80");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            this.Loaded += MainWindow_Loaded;

            getStatusContent();
            getCategorias();
        }

        async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync("/bdevApi/api/index/keyacessoUbasd42123/CategoriaExame");
                response.EnsureSuccessStatusCode(); // Lança um código de erro
                var getData = await response.Content.ReadAsAsync<IEnumerable<Categoria>>();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro : " + ex.Message);
            }
        }
}

I want to receive the information and retrieve it in my var getData, however it is loading the error

Response status code does not indicate success: 406 is not acceptable

I already tried some modified ones in the url but I did not succeed. the problem would be in the url?

Emiry Mirella
  • 567
  • 1
  • 7
  • 21
  • Can you debug the api you are accessing? And try removing the DefaultHeader (or did you send that with postman too?) 406 means the server cannot fulfill your requested answer format ... Maybe the Accept Header is not treated correctly on the server, and postman may not sending it and therefore not trigger the problem – derpirscher Jul 06 '18 at 15:36
  • And be aware, HttpClient is a bit pedantic about baseaddress and request-path. (Although I don't think that's the problem here). To be on the safe side (especially when adding part of the path to the baseaddress and using relative request paths) you *MUST* add a `/` to the end of the baseaddress and *MUST NOT* add a `/` to the beginning of the request-path (https://stackoverflow.com/questions/23438416/why-is-httpclient-baseaddress-not-working) – derpirscher Jul 06 '18 at 15:43
  • I did not define mailman, I'll remove it. I am passing the access key to my api through the url, do you think there is any problem with this? – Emiry Mirella Jul 06 '18 at 16:50
  • 1
    If it's the exact same url like you use in postman, it should not be a problem. But you should try to send *exact the same* request like you do in postman. Ie no extra headers, ... – derpirscher Jul 06 '18 at 16:53
  • that was the problem with the header. Thank you for your help – Emiry Mirella Jul 06 '18 at 16:57
  • can you also help me how do i retrieve the json data item? – Emiry Mirella Jul 06 '18 at 17:01

1 Answers1

1

As commented, remove the Accept header. Seems the API can't handle that correctly.

As for your second request. You can read it as string and parse is with a library like Newtonsoft Json.NET

var json = await content.Response.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<IEnumerable<Categoria>>(json);
derpirscher
  • 14,418
  • 3
  • 18
  • 35