locked
The format of value 'api.vericred.com' is invalid API Endpoint in .Net Core -- RRS feed

  • Question

  • User-215451226 posted

    Hi All.

    This is a typical question and would require knowledge of Vericred medicare API to some extent. Calling the API through .NET Core application. Already registered with the developers account and have relevant key stored in json file.

    Here's the calling code:

    [Route("GetMedicineData/med_name")]
            [HttpGet]
            public async Task<ActionResult<DrugsModel>> GetMedicineData(string med_name)
            {
                APIRequestMaker objAPI = new APIRequestMaker(_config);
                DrugsModel drugsModel = new DrugsModel();
                string apivalue = _config.GetSection("Vericred:API_Key").ToString();
    
                try
                {
                    using (var client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(
                            new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("api.vericred.com"));     *** this line
                        client.DefaultRequestHeaders.Add("Authorization", "API Key" + apivalue);
    
                        var streamTask = client.GetStreamAsync("api.vericred.com/drugs?search_term=" + med_name.ToString());
                        var drugData = await JsonSerializer.DeserializeAsync<DrugsModel>(await streamTask);
    
                        return (drugData);
                    }
                }
                catch(Exception exp)
                {
                    throw exp.GetBaseException();
                }
                
            }

    Here DrugsModel is the corresponding mapped model 
    When you run it, the exception thrown on the line marked is The format of value 'api.vericred.com' is invalid.

    Not sure why that pops up. Things look like I did more or less correctly. If you test via Postman, the API endpoint, you need to select API Key as 'Type' and provide Key-Value as Vericred-Api-Key - blablaablablaaawhatever.

    Simply put I have tried to replicate that in my code. The official docs at Vericred Developer don't contain relevant instructions/guiline.

    Thanks.

    Thursday, May 7, 2020 3:50 PM

Answers

  • User475983607 posted

    You'll need to read the documentation for service you are calling but if this is a REST API it is probably application/json.

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 4:20 PM

All replies

  • User475983607 posted

    You'll need to read the documentation for service you are calling but if this is a REST API it is probably application/json.

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 4:20 PM
  • User711641945 posted

    Hi PGChoudhury,

    Firstly,you need to know the MediaTypeWithQualityHeaderValue constructor needs a string which is a media type.As mgebhard said,if you request  a REST API it is probably application/json.

    Reference:

    https://stackoverflow.com/a/47176966/11398810

    And for adding the request header of authorization,it should be:

    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=xxx");

    Reference:

    https://stackoverflow.com/a/49819111/11398810

    https://stackoverflow.com/a/15242095/11398810

    Best Regards,

    Rena

    Friday, May 8, 2020 8:08 AM
  • User-215451226 posted

    Hi @mgebhard

    Yes, it returns JSON response.
    For eg.

    {
        "meta": {
            "total": 15103
        },
        "drugs": [
            {
                "id": "00832-1670",
                "active_ingredient_strength": "25mg",
                "proprietary_name": "fluvoxamine",
                "non_proprietary_name": "fluvoxamine",
                "drug_package_ids": [
                    "00832-1670-11"
                ]
            },
            {
                "id": "68084-0837",
                "active_ingredient_strength": "50mg",
                "proprietary_name": "fluvoxamine",
                "non_proprietary_name": "fluvoxamine",
                "drug_package_ids": [
                    "68084-0837-25",
             blabblablablaaa..
    ....
    ...
    ..
    .
    ] }

    Reading the fdocu. to find if there are more hints.
    Hi @RenaNi - thanks for suggesting the modif. Will try it out. Revert back accordingly.

    Thanks.

    Friday, May 8, 2020 3:03 PM
  • User-215451226 posted

    Hi again -- an update:

    I am getting the error --> The format of value 'key=blablablaaabla' is invalid. See the screen shot for idea. If you inspect that attributes stack of client, you can see Authorization=null.

    Tried several permutation combinations of quotation marks around the "key=" + apivalue, as initial hunch was, that could have been the case.
    link --> https://imagebin.ca/v/5LpPZklVc0Md

    Friday, May 8, 2020 4:10 PM
  • User475983607 posted

    Your first issue has been solved.  This is a new question.

    PGChoudhury

    I am getting the error --> The format of value 'key=blablablaaabla' is invalid. See the screen shot for idea. If you inspect that attributes stack of client, you can see Authorization=null.

    Tried several permutation combinations of quotation marks around the "key=" + apivalue, as initial hunch was, that could have been the case.
    link --> https://imagebin.ca/v/5LpPZklVc0Md

    Usually your application authenticates with a server and receives an access token.   The token (bearer) is send in subsequent requests which authorizes access to secured resources.  The community has no idea how this service works. You are asking the wrong audience.  Contact the service owners or read the service documentation to figure out what you're supposed to do.  Guessing is not the answer.

    Friday, May 8, 2020 5:27 PM
  • User-215451226 posted

    yeah I know that flow.
    Will ask n seek precise necessary answers on this elsewhere for this specific case.



    Thanks.

    Sunday, May 10, 2020 1:40 PM