0

So I am very new to JSON getting and possting and using these web API's in general. Can someone tell me if they see anything wrong with my code? I am trying to use the gov'ts NPI lookup to get doctors information returned into a JSON object.

Heres the source code:

package API_Playground;

import okhttp3.*;

import java.io.IOException;

public class Main {

    // one instance, reuse
    private final OkHttpClient httpClient = new OkHttpClient();

    public static void main(String[] args) throws Exception {

        Main obj = new Main();

        System.out.println("Testing 1 - Send Http GET request");
        obj.sendGet();

        System.out.println("Testing 2 - Send Http POST request");
        obj.sendPost();

    }

    private void sendGet() throws Exception {

        Request request = new Request.Builder()
                .url("https://npiregistry.cms.hhs.gov/api/?version=2.1")
                .addHeader("first_name", "larry")
                .addHeader("last_name", "mabine")
                .addHeader("state", "GA")
                .build();

        try (Response response = httpClient.newCall(request).execute()) {

            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            // Get response body
            System.out.println(response.body().string());
        }

    }

    private void sendPost() throws Exception {

        // form parameters
        RequestBody formBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("first_name", "john")
                .addFormDataPart("last_name", "smith")
                .addFormDataPart("state", "GA")
                .build();

        Request request = new Request.Builder()
                .url("https://npiregistry.cms.hhs.gov/api?")
                .post(formBody)
                .addHeader("version", "2.0")
                .build();

        System.out.println(request.toString());

        try (Response response = httpClient.newCall(request).execute()) {

            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            // Get response body
            System.out.println(response.body().string());
        }

    }

}

NPI API Information: https://npiregistry.cms.hhs.gov/registry/help-api

Error that I am receiving:

{"Errors":
[
    {
        "description": "Unsupported Version",
        "field": "version",
        "number": "17"
    }
]
JordanV97
  • 1
  • 1
  • 2
    I think that has nothing to do with `json` or `okhttp`, and the error message is clear enough. I am not familiar with NPPES API, but it seems you invoke this API with wrong parameter for `version`. – LHCHIN Jan 15 '20 at 01:03
  • If I modify the request url to include the version, now I get the error ```{"Errors": [{"field": "generic", "number": "04", "description": "No valid search criteria provided"}]}``` – JordanV97 Jan 15 '20 at 01:23
  • Where did you get the response? `sendGet()` or `sendPost()`? – LHCHIN Jan 15 '20 at 02:49
  • Looks like you are trying to pass the request parameters as header, you may refer https://stackoverflow.com/a/33064793/4436914 this code. – shivaji bhosale Jan 15 '20 at 12:20

0 Answers0