简体   繁体   中英

Writing to a 3rd party API

I am working in an asp.net application trying to write a JSON.Net query to write (POST) a record to an API. However, I am having trouble trying to figure out how to format the json string in order to pass it to the API.

The "Example" on the vendor's support page has the following header information.

POST /extact/api/profiles/114226/pages/423833/records HTTP/1.1
Host: server.iPadDataForm.com
Authorization: Bearer 6bfd44fbdcdddc11a88f8274dc38b5c6f0e5121b
Content-Type: application/json
X-IFORM-API-REQUEST-ENCODING: JSON
X-IFORM-API-VERSION: 1.1  

Question:
If I am using JSON.Net, how do I get the header information passed to the API ? I have looked at json.net website , but nothing has worked yet.

JSON.NET is library for serializing and deserializing .NET objects to JSON. It has nothing to do with sending HTTP requests. You could use a WebClient for this purpose.

For example here's how you could call the API:

string url = "http://someapi.com/extact/api/profiles/114226/pages/423833/records";
using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.Authorization] = "Bearer 6bfd44fbdcdddc11a88f8274dc38b5c6f0e5121b";
    client.Headers[HttpRequestHeader.ContentType] = "application/json";
    client.Headers["X-IFORM-API-REQUEST-ENCODING"] = "JSON";
    client.Headers["X-IFORM-API-VERSION"] = "1.1";

    MyViewModel model = ...
    string jsonSerializedModel = JsonConvert.Serialize(model); // <-- Only here you need JSON.NET to serialize your model to a JSON string

    byte[] data = Encoding.UTF8.GetBytes(jsonSerializedModel);
    byte[] result = client.UploadData(url, data);

    // If the API returns JSON here you could deserialize the result
    // back to some view model using JSON.NET
}

The UploadData method will send an HTTP POST request to the remote endpoint. If you want to handle exceptions you could put it in a try/catch block and catch WebException which is what this method could throw if for example the remote endpoint returns some non 2xx HTTP response status code.

Here's how you could handle the exception and read the remote server response in this case:

try
{
    byte[] result = client.UploadData(url, data);
}
catch (WebException ex)
{
    using (var response = ex.Response as HttpWebResponse)
    {
        if (response != null)
        {
            HttpStatusCode code = response.StatusCode;
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string errorContent = reader.ReadToEnd();
            }
        }
    }
}

Notice how in the catch statement you could determine the exact status code returned by the server as well as the response payload. You could also extract the response headers.

Use Web API or the MVC API.

If you want to know the difference.

http://encosia.com/asp-net-web-api-vs-asp-net-mvc-apis/

ASP.NET Web API vs. ASP.NET MVC “APIs” by Dave Ward

In Short the differences are:

Content negotiation Flexibility Separation of concerns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM