Skip to end of metadata
Go to start of metadata

See also this tutorial for more information about using the ASP.NET web API client libraries.

Making a GET request

Let's read orders created after a particular date. For security reasons, query results need to be paged, so we have to specify the $top option (and optionally $skip).

First, we create the message representation.

It looks like:

get
application/json, text/javascript, */*
http://localhost:1260/odata/v1/orders?$top=10&$filter=createdonutc gt datetime'2013-02-20t00:00:00'
2013-11-11T10:15:54.1731069Z
0c6b33651708eb09c8a8d6036b79d739

Now, we can calculate the HMAC signature by using our secret key.

We have all the information to set up the request, so we create a web request object and pass the required headers.

The complete header looks like this:

User-Agent: My shopping data consumer v.1.0
Accept: application/json, text/javascript, */*
Accept-Charset: UTF-8
SmartStore-Net-Api-PublicKey: 0c6b33651708eb09c8a8d6036b79d739
SmartStore-Net-Api-Date: 2013-11-11T10:15:54.1731069Z
Authorization: SmNetHmac1 hWce6V2KA0kkB0GBbIK0GSw5QAcS3+vj+m+WN/8k9EE=

Making a POST request

Posting means inserting data via API. This example shows how to add a new order note "Hello world!" to the order with ID 152. Here is the function to create the MD5 hash of the request body:

No other variables have changed.

We add the same header fields as in the previous example, and additionally: 

Then, we write the content into the request stream.

The message representation is as follows:

post
lgifXydL3FhffpTIilkwOw==
application/json, text/javascript, */*
http://localhost:1260/odata/v1/ordernotes
2013-11-11T19:44:04.9378268Z
0c6b33651708eb09c8a8d6036b79d739

The header looks like this:

User-Agent: My shopping data consumer v.1.0
Accept: application/json, text/javascript, */*
Accept-Charset: UTF-8
SmartStore-Net-Api-PublicKey: 0c6b33651708eb09c8a8d6036b79d739
SmartStore-Net-Api-Date: 2013-11-11T19:44:04.9378268Z
Content-Type: application/json; charset=utf-8
Content-Length: 100
Content-MD5: lgifXydL3FhffpTIilkwOw==
Authorization: SmNetHmac1 ejKxxtHNJYHCtBglZPg+cbSs3YTrA50pkfTHtVb1PMo=

As a general rule, POST, PUT and PATCH are returning the added or changed record. For example:

{ 
"odata.metadata":"http://localhost:1260/odata/v1/$metadata#OrderNotes/@Element",
"OrderId":152,
"Note":"Hello world!",
"DisplayToCustomer":false,
"CreatedOnUtc":"2013-11-09T11:15:00","Id":692
}

Processing the response

Example of reading the response into a string:

JSON data can be easily parsed into dynamic or strongly typed objects using Json.NET. This example deserializes a JSON string into a list of customers.

Dynamic JSON parsing might look like this:

 

 

 

  • No labels