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"
}
]