USDA / USDA-APIs

Do you have feedback, ideas, or questions for USDA APIs? Use this repository's Issue Tracker to join the discussion.
www.usda.gov/developer
109 stars 16 forks source link

Sample Https call for api.nal.usda.gov/fdc/v1/ #68

Closed ghd796 closed 5 years ago

ghd796 commented 5 years ago

I am pretty new to api's but I do use some others without issue. I Am trying to build a simple https request but I can't find any samples. I am currently trying this call but I keep getting errors. what Am I doing wrong: https://api.nal.usda.gov/fdc/v1/?format=json&api_key=MyKey&generalSearchInput=banana

I get the following error: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jun 08 12:35:52 EDT 2019 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "search"

I'm using c# so direct curl doesn't do me much good. I need an http request. This Works fine : https://api.nal.usda.gov/ndb/nutrients/?format=json&api_key=MyKey&nutrients=205&ndbno=01304

littlebunch commented 5 years ago

The call need to be a POST request. Try:

curl -H "Content-Type:application/json"

-d '{"generalSearchInput":"banana"}'

-X POST https://DEMO_KEY@api.nal.usda.gov/fdc/v1/search

Sent from my iPhone

On Jun 8, 2019, at 1:08 PM, gary hengeveld notifications@github.com wrote:

I am pretty new to api's but I do use some others without issue. I Am trying to build a simple https request but I can't find any samples. I am currently trying this call but I keep getting errors. what Am I doing wrong: https://api.nal.usda.gov/fdc/v1/search?format=json&api_key=MyKey&generalSearchInput=banana

I get the following error: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jun 08 12:35:52 EDT 2019 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "search"

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

ghd796 commented 5 years ago

ok,I figured out part of it.

{this Doesn't Work...Why} -In order to look up the Food Search Endpoint the tell you to use: -https://api.nal.usda.gov/fdc/v1/search However I an't get this one to work. My best guess so far has been to use something like this: -https://api.nal.usda.gov/fdc/v1/search?api_key=DEMO_KEY&generalSearchInput=Banana however this give me the following error. and I believe it is because the api is looking for the [FDC ID] and 'search' is not a double

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jun 09 09:56:44 EDT 2019 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "search"

So what am I missing?

sammystolzenbach commented 5 years ago

Hey! I'm also trying to use this and I figured out how to get the food search api working.

use this as the url: "https://api.nal.usda.gov/fdc/v1/search?api_key=YOUR_KEY"

then make sure to send this as the header: headers = { 'Content-Type': 'application/json'}

and this as the data: data = '{"generalSearchInput":"YOUR_SEARCH"}'

so my final request (in python) looked like this: response = requests.post(url, headers=headers, data=data)

Good luck!

ghd796 commented 5 years ago

OK, So this is how I got it to work in c# UWP: Hope this Helps anyone else Attempting to use this API in c#.

    public async static Task<USDARootObject> GetFood(string searchText)
    {

        using (var httpClient = new HttpClient())
        {
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.nal.usda.gov/fdc/v1/search?api_key=DEMO_KEY"))
            {
                request.Content = new StringContent("{\"generalSearchInput\":\"" + searchText + "\"}", Encoding.UTF8, "application/json");

                var response = await httpClient.SendAsync(request);

                var result = await response.Content.ReadAsStringAsync();

                var ser = new DataContractJsonSerializer(typeof(USDARootObject));
                var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

                var data = new USDARootObject();
                data = (USDARootObject)ser.ReadObject(ms);
                return data;
                //dynamic json = JsonConvert.DeserializeObject(result);
            }
        }

    }