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?

6
  • 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 Commented Jul 6, 2018 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 (stackoverflow.com/questions/23438416/…) Commented Jul 6, 2018 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? Commented Jul 6, 2018 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, ... Commented Jul 6, 2018 at 16:53
  • that was the problem with the header. Thank you for your help Commented Jul 6, 2018 at 16:57

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.